外键未更新(配置为必需的主体依赖)

时间:2015-03-02 15:34:49

标签: c# entity-framework ef-code-first

Fluent API配置:

internal class UserMap : EntityTypeConfiguration<User>
    {
        public UserMap()
        {
            Property(x => x.CSL).IsRequired();

            //relational properties
            HasRequired(x => x.UserType).WithMany(x => x.Users).HasForeignKey(x => x.UserType_ID).WillCascadeOnDelete(true);
            HasRequired(x => x.TimelineVolumetry).WithRequiredPrincipal(x => x.User).WillCascadeOnDelete(true);           
        }
    }

internal class TimelineVolumetryMap : EntityTypeConfiguration<TimelineVolumetry>
    {
        public TimelineVolumetryMap()
        {
            Property(x => x.AllowedWeeks).IsRequired();

            //relational properties
            HasRequired(x => x.User).WithRequiredDependent(x => x.TimelineVolumetry).WillCascadeOnDelete(true);
        }
    }

插入查询:

User u = new User();
u.CSL = csl;
u.UserType_ID = role;
u.TimelineVolumetry = new TimelineVolumetry();
u.TimelineVolumetry.User = u;
ctx.User.Add(u);
ctx.SaveChanges();

在数据库中插入用户后,我User_ID = 0获得了TimelineVolumetry。有没有办法在添加用户时自动设置User_ID,或者在设置外键后我被迫再次保存TimelineVolumetry

public class TimelineVolumetry
    {
        public int ID { get; set; }
        public int AllowedWeeks { get; set; }

        //relational properties
        public int User_ID { get; set; }
        public User User { get; set; }

        public TimelineVolumetry()
        {
            AllowedWeeks = 0;
        }
    }

1 个答案:

答案 0 :(得分:0)

我看到一些可以改善你的人际关系配置的要点。首先,您不需要两次配置UserTimelineVolumetry之间的关系,只需一个即可。您也不需要使用级联删除配置关系。如果依赖实体上的外键不可为空(如你的情况),那么Code First会在关系上设置级联删除,因此已经设置了它。

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        Property(x => x.CSL).IsRequired();

        //relational properties
        HasRequired(x => x.UserType).WithMany(x => x.Users).HasForeignKey(x => x.UserType_ID);
        HasRequired(x => x.TimelineVolumetry).WithRequiredPrincipal(x => x.User);           
    }
}

public class TimelineVolumetryMap : EntityTypeConfiguration<TimelineVolumetry>
{
    public TimelineVolumetryMap()
    {
        Property(x => x.AllowedWeeks).IsRequired();
    }
}

关于插入,如果要同时创建两个实体,则应执行以下操作:

User u = new User();
//...
var tv= new TimelineVolumetry();
//set required properties of TimelineVolumetry entity
u.TimelineVolumetry =tv;
//u.TimelineVolumetry.User = u;// you don't need to do this
//...
ctx.User.Add(u);
ctx.SaveChanges();

这将在DB中正确插入两个实体(包括外键列的值)。

更新

我担心你想要的是不可能的。你不能与未被声明为PK的FK建立一对一的关系,实体框架要求依赖的主键也是外键,所以,要实现你想要的,你可以这样做:

public class TimelineVolumetry
{
    public int AllowedWeeks { get; set; }

    //relational properties
    [Key, ForeignKey("User")]
    public int User_ID { get; set; }
    public User User { get; set; }

    public TimelineVolumetry()
    {
        AllowedWeeks = 0;
    }
}

那是使用数据注释,但使用Fluent Api就是这样:

// Configure User_Id as PK for TimelineVolumetry
    modelBuilder.Entity<TimelineVolumetry>()
        .HasKey(e => e.User_Id);

  // Configure User_Id as FK for TimelineVolumetry
    modelBuilder.Entity<User>()
                .HasOptional(s => s.TimelineVolumetry) // Mark TimelineVolumetry is optional for User
                .WithRequired(ad => ad.User); // Create inverse relationship

如果您想了解有关此主题的更多信息,我建议您link