gpt4 book ai didi

entity-framework - 集合的 EF Code First 映射

转载 作者:行者123 更新时间:2023-12-03 05:01:15 25 4
gpt4 key购买 nike

我首先使用 EF 4.1 RC 代码。我在连接表 Friends 中有一个使用复合 PK 的多对多关系。我们明确需要一个单独的 Friends 类(不要问)来代表我们的联结表。我们的目标是能够控制用户实体的删除过程。请在阅读其余部分之前阅读此内容:http://mocella.blogspot.com/2010/01/entity-framework-v4-object-graph.html 。因此,我们设法创建了复合 PK,但这破坏了我们的集合映射。问题是如何映射FriendsCol?

public class User
{
public int UserId { get; set; }
public string Name { get; set; }
public virtual ICollecion<Friends> FriendsCol { get; set; }

}

public class Friends
{
public int User1Id { get; set; }
public int User2Id { get; set; }

public User User1 { get; set; }
public User User2 { get; set; }

}

有一个复合键映射

public class FriendsMap : EntityTypeConfiguration<Friends>
{
HasKey(m => new { m.userId1 , m.userId2 });

//this.HasRequired(x => x.User1)
//.WithMany()
//.HasForeignKey(x => x.User1Id)
//.WillCascadeOnDelete(false);

//this.HasRequired(x => x.User2)
// .WithMany()
// .HasForeignKey(x => x.User2Id)
// .WillCascadeOnDelete(false);
}

public class UserMap : EntityTypeConfiguration<UserNew>
{
public UserMap()
{
ToTable("users");
Property(user => user.Name).HasColumnName("name");
// HasMany<Friends>(user => user.FriendsCol).WithMany();

}
}

最佳答案

这个怎么样:

public class FriendsMap : EntityTypeConfiguration<Friends>
{
HasKey(m => new { m.userId1 , m.userId2 });

this.HasRequired(x => x.User1)
.WithMany()
.HasForeignKey(x => x.User1Id)
.WillCascadeOnDelete(false);

this.HasRequired(x => x.User2)
.WithMany(u => u.FriendsCol)
.HasForeignKey(x => x.User2Id)
.WillCascadeOnDelete(false);
}

public class UserMap : EntityTypeConfiguration<UserNew>
{
public UserMap()
{
ToTable("users");
Property(user => user.Name).HasColumnName("name");
}
}

编辑:

我只是做了一个非常简单的例子,它的工作没有任何问题:

class Program
{
static void Main(string[] args)
{
using (var context = new Context())
{
context.Database.Delete();
context.Database.CreateIfNotExists();

var u1 = new User() { Name = "A" };
var u2 = new User() { Name = "B" };
var u3 = new User() { Name = "C" };

var f1 = new Friends() { User1 = u1, User2 = u2};
var f2 = new Friends() { User1 = u1, User2 = u3 };

context.Friends.Add(f1);
context.Friends.Add(f2);
context.SaveChanges();
}
}
}

public class User
{
public int UserId { get; set; }
public string Name { get; set; }
public virtual ICollection<Friends> FriendsCol { get; set; }
}

public class Friends
{
public int User1Id { get; set; }
public int User2Id { get; set; }

public User User1 { get; set; }
public User User2 { get; set; }

}

public class Context : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Friends> Friends { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<Friends>()
.HasKey(m => new { m.User1Id, m.User2Id });

modelBuilder.Entity<Friends>()
.HasRequired(x => x.User1)
.WithMany()
.HasForeignKey(x => x.User1Id)
.WillCascadeOnDelete(false);

modelBuilder.Entity<Friends>()
.HasRequired(x => x.User2)
.WithMany(u => u.FriendsCol)
.HasForeignKey(x => x.User2Id)
.WillCascadeOnDelete(false);

}
}

关于entity-framework - 集合的 EF Code First 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5472214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com