gpt4 book ai didi

c# - EF6 : Code First Complex Type

转载 作者:可可西里 更新时间:2023-11-01 09:08:57 25 4
gpt4 key购买 nike

我无法让 Entity Framework 将具有值对象(复杂类型)字段的域实体类展平到一个表中。

如果我告诉我的模型构建器忽略我的值对象/复杂类型,一切正常,但这会导致我的表中丢失值对象的所有属性。一旦删除忽略语句,我就会得到“在多个位置创建跨实体共享的值”。如果我查看生成的 CE SQL 文件,我会看到一个额外的表,该表以我的域类命名并附加了 1,并且仅包含值对象参数。

一些代码:

我的领域类:

public User {

private User(){}
public long Id {get; private set;} // dont ask, inherited legacy database
public string UserId { get; private set; }
public string Domain { get; private set; }
public AuditIformation AuditDetails {get ; private set;}

//..domain logic etc
}

public AuditInformation : IValueObject {
public long CreatedByUserId { get; private set; }
public DateTime CreatedDate { get; private set; }
}

我的存储库项目(先写代码)有这个:

public partial class myContext : DbContext {

protected override void OnModelCreating(DbModelBuilder mb) {

mb.Conventions.Remove<PluralizingTableNameConvention>();

mb.ComplexType<Domain.Model.AuditInformation>();
mb.ComplexType<Domain.Model.AuditInformation>().Property(a => a.CreatedDate).HasColumnName("Created_On");
mb.ComplexType<Domain.Model.AuditInformation>().Property(a => a.CreatedByUserId).HasColumnName("Created_By");

//This line lets everything work but doesn't include my
//AuditInformation attributes in my User Table.
mb.Ignore<Domain.Model.AuditInformation>(); // <== I think I need to remove this

//..

mb.Entity<User>().Map(a => {
a.Property(x => x.Id).HasColumnName("Id");
a.Property(x => x.UserId).HasColumnName("User_Id");
a.Property(x => x.Domain).HasColumnName("User_Dmain");
})
.HasKey(x => x.Id)
.ToTable("Tbl_User"); //<==Again, dont ask

}
}

我想要得到的是一张看起来像这样的表格:

[TBL_USER] 
ID AS BIGINT,
USER_ID as VARCHAR(MAX),
USER_DMAIN AS VARCHAR(MAX),
CREATED_ON as DATE,
CREATED_BY as BIGINT

但我得到的只是:

[TBL_USER] 
ID AS BIGINT,
USER_ID as VARCHAR(MAX),
USER_DMAIN AS VARCHAR(MAX),

如果我删除忽略行,我会得到这张奖金怪胎表

[USER1]  <<==Note, named after the domain class, not the destination table.. 
ID AS BIGINT,
CREATED_ON as DATE,
CREATED_BY as BIGINT

当我尝试使用我的存储库时出现一大堆错误:

----> System.Data.Entity.Infrastructure.DbUpdateException : A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns.
----> System.Data.Entity.Core.UpdateException : A value shared across entities or associations is generated in more than one location. Check that mapping does not split an EntityKey to multiple store-generated columns.
----> System.ArgumentException : An item with the same key has already been added.
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.

我进行了大量搜索,但我就是找不到任何将我的值对象属性持久化到为我的域对象创建的表中的具体示例。有人可以告诉我哪里出错了吗?

最佳答案

试试这个:

public class AuditInformation
{
public long CreatedByUserId { get; set; }
public DateTime CreatedDate { get; set; }
}

public abstract class AuditInfo
{
public AuditInformation AuditDetails { get; set; }

public AuditInfo()
{
this.AuditDetails = new AuditInformation();
this.AuditDetails.CreatedByUserId = 0;
this.AuditDetails.CreatedDate = DateTime.Now;
}
}

public User : AuditInfo
{
private User(){}
public long Id {get; private set;} // dont ask, inherited legacy database
public string UserId { get; private set; }
public string Domain { get; private set; }

//..domain logic etc
}

public partial class myContext : DbContext
{
public DbSet<User> Users { get; set; }

protected override void OnModelCreating(DbModelBuilder mb)
{
mb.Conventions.Remove<PluralizingTableNameConvention>();

mb.ComplexType<Domain.Model.AuditInformation>();
mb.ComplexType<Domain.Model.AuditInformation>().Property(a => a.CreatedDate).HasColumnName("Created_On");
mb.ComplexType<Domain.Model.AuditInformation>().Property(a => a.CreatedByUserId).HasColumnName("Created_By");


mb.Entity<Cricketer>().Map(a =>
{
a.Property(x => x.Id).HasColumnName("Id");
a.Property(x => x.UserId).HasColumnName("User_Id");
a.Property(x => x.Domain).HasColumnName("User_Dmain");
a.Property(x => x.AuditDetails.CreatedByUserId).HasColumnName("CreatedByUserId");
a.Property(x => x.AuditDetails.CreatedDate).HasColumnName("CreatedDate");
})
.HasKey(x => x.ID)
.ToTable("Tbl_User"); //<==Again, dont ask
}
}

关于c# - EF6 : Code First Complex Type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21964268/

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