如何在EF中定义2 FK?

时间:2015-03-01 10:17:04

标签: c# entity-framework

  

我在item中有2个user列表,我在项目表中需要2个fk   但它没有用,代码有什么问题

   public class User
        {
            [Key]
            public int id { get; set; }

        public List<item> a  { get; set; }
        public List<item> b { get; set; }


        public User()
        {
            a= new List<item>();
            b = new List<item>();
        }

    }

    public class item
    {
        [Key]
        public int id { get; set; }
        public string name { get; set; }


        [ForeignKey("a")]
        public long aFK { get; set; }

        [ForeignKey("b")]
        public long bFK { get; set; }

        [InverseProperty("a")]
        public virtual User a { get; set; }

        [InverseProperty("b")]
        public virtual User b { get; set; }
    }



    public class usersDB : DbContext
    {
        public DbSet<User> users { get; set; }
        public DbSet<item> items { get; set; }
    }
    public static usersDB dataBase()
    {
        return new usersDB();
    }

   private static void Main(string[] args)
        {        
            using (var db = dataBase())
            {

                db.users.Add(new User() {id = 1});
                db.SaveChanges();
            }
        }

1 个答案:

答案 0 :(得分:0)

  1. 而不是long(在aFK&amp; bFK中),您应该使用int - 输入与外键相同的内容。
  2. 你不能声明两个不可为空的外键,因为默认行为是CascadeOnDelete,在这种情况下是不可能的。所以你必须将它们(aFK&amp; bFK)设置为可空(int?),或通过Flunet-Api设置WillCascadeOnDelete(false)
  3. 顺便说一下,如果你想设置ID yoyrself(id = 1),你需要避免默认配置:DatabaseGeneratedOption.Identity,按属性[DatabaseGenerated(DatabaseGeneratedOption.Identity )]

    所以,这段代码工作:

    class Program
    {
        public static usersDB dataBase()
        {
            return new usersDB();
        }
    
        static void Main(string[] args)
        {
            Database.SetInitializer(new DropCreateDatabaseAlways<usersDB>());
    
            using (var db = dataBase())
            {
                var user = new User() { id = 1};
    
                user.a.Add(new item());
                user.a.Add(new item());
                user.a.Add(new item());
    
                user.b.Add(new item());
                user.b.Add(new item());
    
                db.users.Add(user);
    
                db.SaveChanges();
            }
    
            using (var db = dataBase())
            {
                var user = db.users.Find(1);
                Console.WriteLine(user.a.Count); //OUT: 3
                Console.WriteLine(user.b.Count); //OUT: 2
            }
            Console.ReadLine();
        }
    }
    
    
    public class User
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity )]
        public int id { get; set; }
    
        public virtual List<item> a { get; set; }
        public virtual List<item> b { get; set; }
    
        public User()
        {
            a = new List<item>();
            b = new List<item>();
        }
    }
    
    public class item
    {
        [Key]
        public int id { get; set; }
    
        public string name { get; set; }
    
        [ForeignKey("a")]
        public int? aFK { get; set; }
    
        [ForeignKey("b")]
        public int? bFK { get; set; }
    
        [InverseProperty("a")]
        public virtual User a { get; set; }
    
        [InverseProperty("b")]
        public virtual User b { get; set; }
    }
    
    public class usersDB : DbContext
    {
        public DbSet<User> users { get; set; }
        public DbSet<item> items { get; set; }
    }