gpt4 book ai didi

entity-framework - 在 Entity Framework 中更改表名(代码优先)

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

上下文如下:

一个人可能属于一个组(他将是一个成员)并且可能是一个组的管理员。
这在代码优先中是这样建模的:

class Group
{
[InverseProperty("GroupsWhereIamAdmin")]
public virtual ICollection<Person> Admins {get; set;}
[InverseProperty("GroupsWhereIamMember")]
public virtual ICollection<Person> Members {get; set;}
}

class Person
{
[InverseProperty("Members")]
public virtual ICollection<Group> GroupsWhereIamMember {get; set;}
[InverseProperty("Admins")]
public virtual ICollection<Group> GroupsWhereIamAdmin {get; set;}
}

问题是生成的表的名称是这些:

团体人
组人1

我希望他们是:

组管理员
集团成员

我怎样才能以简单的方式实现这一目标? (即:使用属性)

最佳答案

所以我想这必须用 Fluent API 来完成,
所以我删除了数据注释,即:[InverseProperty(...)]'s,然后这样做:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Many to many: A group has members, and a person has groups.
modelBuilder.Entity<Group>()
.HasMany(e => e.Members)
.WithMany(m => m.GroupsWhereIAmMember)
.Map(mc =>
{
mc.ToTable("GroupMembers");
mc.MapLeftKey("GroupId");
mc.MapRightKey("MemberId");
});

// Many to many: A group has admins, and a person has groups where he is admin
modelBuilder.Entity<Group>()
.HasMany(e => e.Admins)
.WithMany(m => m.GroupsWhereIAmAdmin)
.Map(mc =>
{
mc.ToTable("GroupAdmins");
mc.MapLeftKey("GroupId");
mc.MapRightKey("AdminId");
});
}

关于entity-framework - 在 Entity Framework 中更改表名(代码优先),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15851728/

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