小编JK.*_*JK.的帖子

实体框架非可空列映射到可空实体属性

我在实体框架4.1中获得这个可空列错误消息 - 当列实际上不可为空时:

Non-nullable column MyView.RunningTotal in table TransactionListView 
is mapped to a nullable entity property.
Run Code Online (Sandbox Code Playgroud)

但是您可以从下面的屏幕截图中看到该列不可为空,并且实体属性也不可为空.那么为什么会出现这种错误呢?

在此输入图像描述

c# mapping entity-framework nullable poco

8
推荐指数
1
解决办法
9526
查看次数

Dapper的IEnumerable <T>是否已推迟或立即执行?

当我在Dapper中执行查询并且只想要检索一个记录块时,我可以使用.Skip().Take(),还是需要在SQL中使用select top n*?

例如,给定一个包含10,000条记录的表,我只想要前200条,因为我的列表页面每页只显示200条.我跑这个吗?

conn.Query<Widget>("select * from Widgets").Skip((page - 1) * size).Take(size);
Run Code Online (Sandbox Code Playgroud)

或这个:

conn.Query<Widget>("select top 200 * from Widgets");
Run Code Online (Sandbox Code Playgroud)

Dapper的.Query<T>方法是否延期?

c# ienumerable orm deferred-execution dapper

8
推荐指数
1
解决办法
1738
查看次数

如何在格式字符串中设置占位符的颜色

我在Visual Studio中安装了Resharper,并使用标准的"黑暗"主题,不知怎的,我设法改变了一些东西,使得格式字符串中的"{0}"字符不再与字符串的其余部分有不同的颜色.

例如,在以下行中:

Console.WriteLine("Hello {0}", Environment.UserName);
Run Code Online (Sandbox Code Playgroud)

引用的字符串完全是相同的锈色(如此SO编辑器中所示),而之前的"{0}"以亮绿色突出显示.

编辑

"Resharper Format String Item"下的设置显示正确:

在此输入图像描述

此外,R#Code Annotations设置为JetBrains.Annotations,并设置为默认值:

在此输入图像描述

其他一些设置会覆盖这个吗?有没有一种简单的方法可以将所有内容重置为默认值?

c# resharper color-scheme visual-studio

8
推荐指数
1
解决办法
1132
查看次数

调整大小时,锐利图像库会旋转图像吗?

对于node.js 使用清晰图像调整大小库https://github.com/lovell/sharp时,图像正在旋转.

我没有代码说.rotate(),为什么它被旋转,我怎么能阻止它旋转?

我使用的是由AWS提供的无服务器,图像缩放例如: https://github.com/awslabs/serverless-image-resizing使用拉姆达在飞行中调整图像如果缩略图不存在

S3.getObject({Bucket: BUCKET, Key: originalKey}).promise()
.then(data => Sharp(data.Body)
      .resize(width, height)
      .toFormat('png')
      .toBuffer()
    )
.then(buffer => S3.putObject({
        Body: buffer,
        Bucket: BUCKET,
        ContentType: 'image/png',
        Key: key,
      }).promise()
    )
.then(() => callback(null, {
        statusCode: '301',
        headers: {'location': `${URL}/${key}`},
        body: '',
      })
    )
.catch(err => callback(err))
Run Code Online (Sandbox Code Playgroud)

原始大图:

在此输入图像描述

已调整大小的图片:请注意它已被旋转:

在此输入图像描述

amazon-s3 image-resizing node.js aws-lambda sharp

8
推荐指数
2
解决办法
2144
查看次数

c#字符串操作

我有9个字母的字符串.

string myString = "123987898";
Run Code Online (Sandbox Code Playgroud)

我想要检索前3个字母"123"然后再检索2个字母"98",然后再检索4个字母"7898".

哪个c#字符串函数支持此功能.

c# string

7
推荐指数
1
解决办法
2708
查看次数

ASPNET MVC - 使用具有相同签名的新助手覆盖Html.TextBoxFor(model.property)?

我想用我自己的帮助器覆盖Html.TextBoxFor(),它有完全相同的签名(当然是一个不同的命名空间) - 这是可能的,如果是的话,怎么样?

这样做的原因是我在现有应用程序中有100多个视图,我想改变TextBoxFor的行为,以便在属性具有[StringLength(n)]注释时输出maxLength = n属性.

自动输出maxlength = n的代码在这个问题中:来自Asp.Net MVC中DataAnnotations StringLength的文本框的maxlength属性.但我的问题并不重复 - 我正在尝试创建一个更通用的解决方案:DataAnnotaion自动流入html,而不需要编写视图的人员需要额外的代码.

在引用的问题中,您必须将每个Html.TexBoxFor更改为Html.CustomTextBoxFor.我需要这样做,以便不需要更改现有的TextBoxFor() - 因此创建一个具有相同签名的帮助程序:更改帮助程序方法的行为,并且所有现有实例将在没有任何更改的情况下正常工作(100 + views,至少500 TextBoxFor()s - 不想手动编辑它).

我试过这段代码:(并且我需要为TextBoxFor的每次重载重复它,但是一旦根问题解决了,那将是微不足道的)

namespace My.Helpers
{
    public static class CustomTextBoxHelper
    {
        public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes, bool includeLengthIfAnnotated)
        {
                // implementation here
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我在Html.TextBoxFor()视图中遇到编译器错误:"调用在以下方法或属性之间是不明确的"(当然).有没有办法做到这一点?

是否有一种替代方法可以让我改变Html.TextBoxFor的行为,以便不需要更改已经使用它的视图?

c# asp.net-mvc html-helper data-annotations

7
推荐指数
1
解决办法
6266
查看次数

从数据库加载没有代理类?

在实体框架4中是否可以选择在没有使用代理类的情况下将某些查询加载到POCO中?(出于缓存该对象的目的,以备将来只读使用).我正在使用存储库 - 服务模式.

我的意思是:

var order = _orderService.GetById(1);
// after order is loaded then we can see in the debugger that:
// order.Customer is of type System.Data.Entity.DynamicProxy.Customer_17631AJG_etc
Run Code Online (Sandbox Code Playgroud)

我想要的是order.Customer实际使用POCO类型MyApp.Models.Entities.Customer而不是该类型的代理.

编辑:根据Ladislav建议在存储库中添加"GetUnproxied"方法,我做了这个改动:

// this is the current method that must return a DynamicProxy
public IQueryable<T> GetQuery()
{
    return ObjectSet.AsQueryable();
}

// this is the new additional method that must return the plain POCO
public IQueryable<T> GetReadOnly()
{
    ObjectContext.ContextOptions.ProxyCreationEnabled = false;
    var readOnly = ObjectSet.AsQueryable();
    ObjectContext.ContextOptions.ProxyCreationEnabled = true;
    return readOnly; …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework proxy-classes poco

7
推荐指数
1
解决办法
9216
查看次数

C#克隆System.Data.Entity.DynamicProxies到实际(非代理)类?

可能重复:
EF4 Cast DynamicProxies到底层对象

我试图找出如何克隆或将System.Data.Entity.DynamicProxies转换为它的实际类.例如:

System.Data.Entity.DynamicProxies.Currency_F4008E27DE_etc is the proxy class
MyApp.Entities.Currency is the real class
Run Code Online (Sandbox Code Playgroud)

MyApp.Entities中的所有类都继承自BaseEntity,因此我尝试在那里进行转换:

public abstract partial class BaseEntity
{
    public T ShallowCopy<T>() where T : BaseEntity
    {
        return this.MemberwiseClone() as T;
    }
    // other BaseEntity properties not relevent here
}
Run Code Online (Sandbox Code Playgroud)

然后将DynamicProxies转换为真正的类:

// this returns a DynamicProxies class
Currency currency = LookupDefaultCurrency(); 
// this one needs to return a Entities.Currency class 
// (but currently returns a DynamicProxies class too
Currency pocoCurrency = (Currency)currency.ShallowCopy<Currency>();
HttpRuntime.Cache[key] = pocoCurrency;
Run Code Online (Sandbox Code Playgroud)

这样做的原因是我想从该对象中删除所有Entity Framework跟踪等,并将其普通(POCO)属性存储在缓存中.我需要能够为所有100个左右的Entity类执行此操作,因此它必须具有合理的通用性 …

c# entity-framework poco dynamic-proxy cloning

7
推荐指数
1
解决办法
9674
查看次数

设置IIS7 gzip压缩级别

在ASP.NET MVC中,我使用web.config来启用和配置IIS7.5 gzip压缩设置.但是他们压缩级别设置似乎根本没有效果:

<scheme name="gzip" dynamicCompressionLevel="9" staticCompressionLevel="9"/>
Run Code Online (Sandbox Code Playgroud)

对于两个设置,压缩级别= 0,我的主页被压缩到9,290字节(从39,623)

两个设置的压缩级别= 9,我的主页被压缩到9,290字节(从39,623)

(使用fiddler检查压缩/未压缩的尺寸)

压缩量没有差别 - 为什么?这发生在我的本地开发机器上 - Windows 7.我还没有在我们的Win 2008 Web服务器上尝试过它.

web.config中的完全压缩设置:

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" dynamicCompressionLevel="10" staticCompressionLevel="10"/>
  <dynamicTypes>
    <add mimeType="text/*" enabled="true"/>
    <add mimeType="message/*" enabled="true"/>
    <add mimeType="application/javascript" enabled="true"/>
    <add mimeType="application/x-javascript" enabled="true"/>
    <add mimeType="application/xml" enabled="true"/>
    <add mimeType="*/*" enabled="false"/>
  </dynamicTypes>
  <staticTypes>
    <add mimeType="text/*" enabled="true"/>
    <add mimeType="message/*" enabled="true"/>
    <add mimeType="application/javascript" enabled="true"/>
    <add mimeType="application/x-javascript" enabled="true"/>
    <add mimeType="application/xml" enabled="true"/>
    <add mimeType="*/*" enabled="false"/>
  </staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>
Run Code Online (Sandbox Code Playgroud)

编辑:显然最高级别为9.此页面显示它是10但必须不正确http://www.iis.net/configreference/system.webserver/httpcompression/scheme.使用9级时问题仍然存在

compression iis asp.net-mvc iis-7 gzip

7
推荐指数
1
解决办法
7873
查看次数

渲染视图时是否会发生LINQ延迟执行,或者更早?

给定一个返回MVC视图的非常基本的LINQ,延迟执行在什么时候开始执行?

在控制器中:

public ActionResult Index()
{
    var model = _fooService.GetAll();
    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

在模型中:

@foreach (var item in Model) {    
    <tr>
        <td>@item.Bar</td>
   </tr>
}
Run Code Online (Sandbox Code Playgroud)

当我们调用时,查询不会被执行_fooService.GetAll(),但是会延迟到稍后的某个时间点 - 但是它执行的确切位置是什么?

  • return View(model);控制器中的语句(看起来不像)?
  • @foreach (var item in Model)视图中的线?
  • 第一次@item.Bar看到视图中的线?
  • 介于两者之间的其他东西return View(model);和正在呈现的视图?

c# linq asp.net-mvc deferred-execution

7
推荐指数
2
解决办法
681
查看次数