实体框架可以查询数据,但不能保存



我使用EF5,我有一些我自己写的实体,还写了一个函数,将所有映射添加到modelbuilder配置。

运行一个简单的测试查询,我可以成功地从表中查询项目,但是当我尝试添加一个新项目并保存时,我得到一个异常,我的实体的主键是空的,即使我给了它一个值。

很有可能我搞砸了映射,但我不知道为什么它会为查询工作,而不是保存。

public class User : IMappedEntity 
{
    [Key]
    [Column("USER_ID")]
    public int UserID { get; set; }
    [Column("FIRST_NAME")]
    public String FirstName { get; set; }
    [Column("LAST_NAME")]
    public String LastName { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingEntitySetNameConvention>();
        modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
    var addMethod = (from m in (modelBuilder.Configurations).GetType().GetMethods()
                     where m.Name == "Add" 
                        && m.GetParameters().Count() == 1
                        && m.GetParameters()[0].ParameterType.Name == typeof(EntityTypeConfiguration<>).Name
                     select m).First();
    if(mappings != null)
    {
        foreach(var map in mappings)
        {
            if(map != null && !mappedTypes.Contains(map.GetType()))
            {
                var thisType = map.GetType();
                if (thisType.IsGenericType)
                {
                    thisType = map.GetType().GenericTypeArguments[0];
                }
                var thisAddMethod = addMethod.MakeGenericMethod(new[] {thisType});
                thisAddMethod.Invoke(modelBuilder.Configurations, new[] { map });
                mappedTypes.Add(map.GetType());
            }
        }
    }
}
private List<Object> BuildMappings(IEnumerable<Type> types)
{
    List<Object> results = new List<Object>();
    var pkType = typeof(KeyAttribute);
    var dbGenType = typeof(DatabaseGeneratedAttribute);
    foreach (Type t in types)
    {
        String tableName = GetTableName(t);
        String schemaName = GetSchema(t);
        var mappingType = typeof(EntityTypeConfiguration<>).MakeGenericType(t);
        dynamic mapping = Activator.CreateInstance(mappingType);
        if (!String.IsNullOrWhiteSpace(schemaName))
           mapping.ToTable(tableName, SchemaName.ToUpper());
        else
           mapping.ToTable(tableName);
        var keys = new List<PropertyInfo>();
        foreach (PropertyInfo prop in t.GetProperties())
        {
            String columnName = prop.Name;
            if(Attribute.IsDefined(prop, typeof(ColumnAttribute)))
            {
                columnName  = (prop.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute).Name;
            }
            if(Attribute.IsDefined(prop, pkType))
               keys.Add(prop);
            var genFunc = (typeof(Func<,>)).MakeGenericType(t, prop.PropertyType);
            var param = Expression.Parameter(t, "t");
            var body = Expression.PropertyOrField(param, prop.Name);
            dynamic lambda = Expression.Lambda(genFunc, body, new ParameterExpression[] { param });
            //if (prop.PropertyType == typeof(Guid) || prop.PropertyType == typeof(Nullable<Guid>))
            //{
            //    mapping.Property(lambda).HasColumnType("Guid");
            //}
            //else
            mapping.Property(lambda).HasColumnName(columnName);
            if (Attribute.IsDefined(prop, dbGenType))
               mapping.Property(lambda).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
        }
        if (keys.Count == 0)
           throw new InvalidOperationException("Entity must have a primary key");
        dynamic entKey = null;
        if(keys.Count == 1)
        {
            var genFunc = (typeof(Func<,>)).MakeGenericType(t, keys[0].PropertyType);
            var param = Expression.Parameter(t, "t");
            var body = Expression.PropertyOrField(param, keys[0].Name);
            entKey = Expression.Lambda(genFunc, body, new ParameterExpression[] { param });
        }
        else
        {
            //if entity uses a compound key, it must have a function named "GetPrimaryKey()" which returns Expression<Func<EntityType,Object>>
            //this is because I can't create an expression tree that creates an anonymous type
            entKey = t.GetMethod("GetPrimaryKey");
        }
        mapping.HasKey(entKey);
        results.Add(mapping);
    }
    return results;
}
static void Main(string[] args)
{
    using (var ctx = new DQSA.Data.DBContext("DQSATEST"))
    {
        var xxx = (from u in ctx.Query<DQSA.Data.Entities.User>()
                   select u).ToList(); //this works, I can see my user
        ctx.Set<DQSA.Data.Entities.User>().Add(new DQSA.Data.Entities.User()
            { UserID = 0,
              FirstName="Sam",
              LastName="Sam"
            });
        ctx.SaveChanges(); //get an exception here
        xxx = (from u in ctx.Query<DQSA.Data.Entities.User>()
               select u).ToList();
    }
}

看起来你的UserID属性被映射为一个身份列约定,所以EF查询提供者认为它不需要插入该值和数据库抱怨,因为该字段是不可空的。

您可以使用DatabaseGeneratedAttribute

来覆盖模型中的约定
public class User : IMappedEntity 
{
    [Key]
    [Column("USER_ID")]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int UserID { get; set; }
    ...
}

或者通过全局删除约定(在DbContext的OnModelCreating()方法中)…

modelBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>();

我认为您需要尝试与一个或几个记录播种然后context.SaveChanges()

默认情况下,实体框架应该将使用Code First创建的新表的主键列标记为标识列。在编写代码之前,数据库是否已经存在,还是您先使用代码创建它?

您能否在Management Studio中验证该列是否为该字段打开了标识?

最新更新