Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 实体框架4 POCO中的默认SQL Server列值_.net_Database_Entity Framework_Poco - Fatal编程技术网

.net 实体框架4 POCO中的默认SQL Server列值

.net 实体框架4 POCO中的默认SQL Server列值,.net,database,entity-framework,poco,.net,Database,Entity Framework,Poco,我已从包含默认列值的现有数据库生成EDMX。我还使用T4模板生成了POCO对象 现在我有一个场景,我想创建一个POCO并将其保存到数据库中,如下所示: dim tablePocoEntityInstance as New tablePocoEntity context.MsSQLTable.AddObject(tablePocoEntityInstance) context.SaveChanges() partial void OnCreationComplete(); 除了在SQL Ser

我已从包含默认列值的现有数据库生成EDMX。我还使用T4模板生成了POCO对象

现在我有一个场景,我想创建一个POCO并将其保存到数据库中,如下所示:

dim tablePocoEntityInstance as New tablePocoEntity
context.MsSQLTable.AddObject(tablePocoEntityInstance)
context.SaveChanges()
partial void OnCreationComplete();
除了在SQL Server数据库中设置的默认值外,这一切正常

例如:

SQL Server表

  id (int, not null, auto increament)
  magicNR (int, not null, defaultValue = 11)
生成的POCO对象有两个属性:

Partial Public Class tablePocoEntity
    Public Overridable Property id As Integer
    Public Overridable Property magicNR As Integer
...
问题是
magicNR
不可为null,并且隐式初始化。当我保存对象时,
id
与它应该的一样,但是
magicNR
的值为0,而不是默认值11

我的问题是:

  • 我是否可以将实体设置为使用数据库默认列值
  • 如果没有,如何在代码中设置默认值
  • 处理这个问题的最好办法是什么

  • 我知道没有现成的解决方案,但由于这些POCO类是使用T4模板从数据库生成的,因此您可以随时修改这些模板以检查数据库并查找和遵守列默认值


    在这种情况下,您的对象可能会有一个构造函数,将这些列设置为数据库定义定义的默认值。

    打开entity.edmx,右键单击有问题的数据字段并选择properties。在DataBaseScriptGeneration下,将StoreGeneratedPattern更改为“Computed”。设置后,您的应用程序将按预期工作-SQL Server将插入默认值。但是,您将无法从应用程序端强制输入值。

    好的,这个问题已经很老了,但我还是想分享我的解决方案

    我所做的是修改T4模板,以便向所有生成的实体添加partial方法,并从构造函数调用它。 此分部方法在扩展分部类中实现,您将为需要设置默认值的每个实体手动创建该分部类

    注意:我使用的是EF6

    简短步骤:

    1) 修改T4模板以包含如下部分方法:

    dim tablePocoEntityInstance as New tablePocoEntity
    context.MsSQLTable.AddObject(tablePocoEntityInstance)
    context.SaveChanges()
    
    partial void OnCreationComplete();
    
    2) 修改T4模板以在构造函数中调用该方法

    OnCreationComplete();
    
    3) 为需要设置默认属性并实现OnCreationComplete方法的实体创建分部类:

    partial void OnCreationComplete()
    {
        PropertyFoo = "Bar";
    }
    
    以下是完整的代码:

    T4模板

    // You might want to remove the IF statement that excludes the constructor generation for entities without collection and complex entities...
    <#
        var propertiesWithDefaultValues = typeMapper.GetPropertiesWithDefaultValues(entity);
        var collectionNavigationProperties = typeMapper.GetCollectionNavigationProperties(entity);
        var complexProperties = typeMapper.GetComplexProperties(entity);
    #>
        public <#=code.Escape(entity)#>()
        {
    <#
            foreach (var edmProperty in propertiesWithDefaultValues)
            {
    #>
            <#=code.Escape(edmProperty)#> = <#=typeMapper.CreateLiteral(edmProperty.DefaultValue)#>;
    <#
            }
    
            foreach (var navigationProperty in collectionNavigationProperties)
            {
    #>
            <#=code.Escape(navigationProperty)#> = new HashSet<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
    <#
            }
    
            foreach (var complexProperty in complexProperties)
            {
    #>
            <#=code.Escape(complexProperty)#> = new <#=typeMapper.GetTypeName(complexProperty.TypeUsage)#>();
    <#
            }
    #>
            OnCreationComplete();
        }
    
        partial void OnCreationComplete();
    
    希望它能帮助别人