【发布时间】:2012-04-13 06:10:32
【问题描述】:
我有两个表(表 A、表 B)与一个包含 3 个有效负载列的连接表 (TableAB) 连接。有效负载是指除 Id、TableAId 和 TableBId 之外的列。
我可以成功插入所有表,但我需要将数据插入到 Insert 上的有效负载列之一。我正在使用 EF 4.3,Fluent API。任何人都可以帮忙吗?提前致谢。
public class Organisation : EntityBase<int>, IAggregateRoot
{
public string Name { get; set; }
public string Url { get; set; }
public int CountryId { get; set; }
public int? OwnershipTypeId { get; set; }
public int OrganisationStatusId { get; set; }
public virtual ICollection<Feature> Features { get; set; }
public virtual ICollection<OrganisationType> OrganisationTypes { get; set; }
public virtual ICollection<PricePlan> PricePlans { get; set; }
public virtual ICollection<User> Users { get; set; }
}
public class User: EntityBase<Guid>, IAggregateRoot
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string JobTitle { get; set; }
public int? PhoneCallingCodeId { get; set; }
public int? PhoneAreaCode{ get; set; }
public string PhoneLocal { get; set; }
public int? MobileCallingCodeId { get; set; }
public int? MobileAreaCode { get; set; }
public string MobileLocal { get; set; }
public virtual ICollection<Organisation.Organisation> Organisations { get; set; }
}
public class OrganisationUser : EntityBase<int>, IAggregateRoot
{
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public int OrganisationRoleId {get; set;}//Foreign Key - have tried leaving it out, tried it as public virtual Organisation Organisation {get;set;
public bool IsApproved { get; set; }
}
public class SDContext : DbContext
{
public ObjectContext Core
{
get
{
return (this as IObjectContextAdapter).ObjectContext;
}
}
public IDbSet<User> User { get; set; }
public IDbSet<Organisation> Organisation { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Organisation>().HasMany(u => u.Users).WithMany(o => o.Organisations).Map(m =>
{
m.MapLeftKey("OrganisationId");
m.MapRightKey("UserId");
m.ToTable("OrganisationUser");
});
//我已经尝试在fluent中专门定义外键,但是我真的需要了解如何在访问和编辑它们后添加有效负载属性。
【问题讨论】:
-
能否请您展示您拥有的类(尤其是它们的导航属性)和 Fluent 映射?
-
嗨,这真的很重要吗?我知道这些表与 POCO 正确映射,因为 ID 的所有插入都很好。我只是不知道如何访问有效负载列。假设我有一个组织,我正在添加用户、计费计划等,所以我正在查询相关实体,在 foreach 循环中使用 organization.Add(entity) 将集合添加到我的组织中。如何访问有效负载的联接表 - 我已经为联接创建了一个 POCO 类。
-
是的,这确实很重要,因为围绕您的问题的长时间沉默证明了这一点。直到现在您才得到答案,因为您没有提供必要的详细信息。最后一条语句很重要——你有一个连接表的实体,它会有你的有效负载列,对吧?那么问题出在哪里呢?加载、更改、保存...您可以编辑您的问题(问题下方的“编辑”链接)以添加更多信息。
-
您不需要发布您的真实代码,只需举例一些与您的真实代码具有相同结构的代码。您可以使用
TableXYZ之类的傻瓜,但重要的是要知道您拥有哪些实体和导航以及 FK 属性,它是如何在 Fluent API 中映射的,等等。 -
var jointable = new OrganisastionUser{Id=xx, organizationId=organisation.id, userId=user.Id, roleId=1}; xxxx.Add(jointable) - XXXXXXX 是什么???
标签: entity-framework insert many-to-many jointable payload