基于视图生成实体框架:可空类型 [英] Generating Entity Framework based on Views: Nullable types

查看:111
本文介绍了基于视图生成实体框架:可空类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由EF生成的类中的某些属性为空,有些不为空。

Some of the properties in a class generated by EF are nullable, and some arent.

我的第一个直觉是这应该由sp_help MyView返回的Nullable属性驱动。但似乎并非如此。

My first instinct was that this should be driven by Nullable property returned by sp_help MyView. But that doesn't seem to be the case.

由sp_help返回为Nullable的某些类型将生成为Nullable,而其他类型则生成为bool而不是Nullable * bool *

Some of my types that are returned as Nullable by sp_help get generated as Nullable , while others, get generated as just bool instead of Nullable*bool*

是什么驱动,有什么办法控制它的视图?

what is it driven by, and is there any way to control it by the view?

作为一个测试,我创建了ViewA和比ViewB选择ViewA中的所有列。令我惊讶的是,从这些观点创造的实体并不完全相同。某些属性仅在ViewB中为空。

as a test i created ViewA and than ViewB which selected all the columns from ViewA. To my astonishment, entities created from those views were not identical. Some properties were nullable only in ViewB.

推荐答案

您的预感是正确的,EF根据您的存储模式创建Nullable属性,而Views也不例外。换句话说,生成的实体对象应该(并将)完全反映您的View模式,并且有一种方法可以找到问题:



首先需要确保数据库视图已正确导入到您的模型中:


为此,请在XML编辑器中打开您的EDM,转到 SSDL内容并找到< EntityType Name =yourDbViewName> 然后查看< Property Name =yourFieldNameType =intNullable =false /> 确保每个字段对 Nullable 属性具有正确的值。由于默认值为true,所以EF只会将此属性设置为 NOT Null 字段。




从EF4起,VS2010使用T4模板生成实体对象。钻入T4可以显示如何在Nullablity中生成对象:

Your hunch is correct, EF create Nullable properties based on your store schema and Views are not an exception to this. In other words, the generated entity object should (and will) exactly reflect your View schema and there is a way that you can find the problem:

First you need to make sure that the DB View has been correctly imported to your model:
For that, crack open your EDM in the XML Editor, go to the SSDL content and find <EntityType Name="yourDbViewName"> then look at the <Property Name="yourFieldName" Type="int" Nullable="false" /> make sure every field has the correct value for Nullable attribute. Since the default value is true, EF, will only put this attribute if it's a NOT Null field.

As of EF4, VS2010 uses a T4 template to generate Entity Objects. Drilling into this T4 reveals how the objects being generated in terms of Nullablity:

private void WritePrimitiveTypeProperty(EdmProperty primitiveProperty, CodeGenerationTools code)
    {
        MetadataTools ef = new MetadataTools(this);
#>

    /// <summary>
    /// <#=SummaryComment(primitiveProperty)#>
    /// </summary><#=LongDescriptionCommentElement(primitiveProperty, 1)#>
    [EdmScalarPropertyAttribute(EntityKeyProperty=<#=code.CreateLiteral(ef.IsKey(primitiveProperty))#>, IsNullable=<#=code.CreateLiteral(ef.IsNullable(primitiveProperty))#>)]
    [DataMemberAttribute()]
    <#=code.SpaceAfter(NewModifier(primitiveProperty))#><#=Accessibility.ForProperty(primitiveProperty)#> <#=code.Escape(primitiveProperty.TypeUsage)#> <#=code.Escape(primitiveProperty)#>

如您所见,EF使用MetadataTools 来确定属性是否为Nullable,而MetadataTools 基本上包含帮助程序访问代码生成所需的实体框架元数据意味着它会查看您的EDM以获取此信息。当然,您可以更改此模板并进行自定义,但是为了根据您的数据库视图来控制Nullable类型,您不需要,因为它已经存在。

As you can see EF uses MetadataTools to determine if the properties are Nullable and MetadataTools basically contains helper methods that access the Entity Framework metadata needed for code generation which means it looks at your EDM to get this information. Of course you can change this template and customize it, but for the intention of controlling Nullable types based on your DB View, you don't need to since it's already there.

这篇关于基于视图生成实体框架:可空类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆