在我的测试应用程序中有以下类:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
Database.SetInitializer<ApplicationDbContext>(new MovieContextInitializer());
}
public DbSet<Movie> Movies { get; set; }
public DbSet<Currency> Currencies { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Types<Movie>()
.Configure(ctc => ctc.Property(m => m.Price.Amount).HasColumnName("MoviePriceAmount"));
modelBuilder.Types<Movie>()
.Configure(ctc => ctc.Property(m => m.Price.Currency.Id).HasColumnName("MoviePriceCurrency"));
base.OnModelCreating(modelBuilder);
}
}
MovieContextInitializer for init data:
public class MovieContextInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext>
{
protected override void Seed(ApplicationDbContext context)
{
var usdCurrency = new Currency {Id = 1, Name = "USD"};
var euroCurrency = new Currency {Id = 2, Name = "EUR"};
context.Currencies.Add(usdCurrency);
context.Currencies.Add(euroCurrency);
context.Movies.Add(new Movie
{
Title = "GhoustBusters",
Genre = "Comedy",
ReleaseDate = new DateTime(2016, 9, 10),
Price = new Money(50, usdCurrency)
});
context.Movies.Add(new Movie
{
Title = "Matrix",
Genre = "Action",
ReleaseDate = new DateTime(2010, 2, 10),
Price = new Money(40, euroCurrency)
});
}
}
我的实体和值对象类:
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string Genre { get; set; }
public Money Price { get; set; }
}
public class Currency
{
public int Id { get; set; }
public string Name { get; set; }
}
[ComplexType]
public class Money
{
public Money(int amount, Currency currency)
{
Amount = amount;
Currency = currency;
}
public int Amount { get; private set; }
public Currency Currency { get; private set; }
}
当我试图打电话时:
ApplicationDbContext db = new ApplicationDbContext();
var movies = db.Movies.ToList()
我得到异常:"在模型生成期间检测到一个或多个验证错误:DomainDrivenWeb.Models.Currency: Name:模式中的每个类型名称必须是唯一的。类型名称'Currency'已经定义。".
这里的问题是,您将Currency
视为一个ValueObject和一个实体。
:
modelBuilder.Types<Movie>()
.Configure(ctc => ctc.Property(m => m.Price.Currency.Id).HasColumnName("MoviePriceCurrency"));
告诉EF, Currency
类型是Move实体上隐式的复杂类型——嵌套在Price属性(Money
类型)上。这更像是一个ValueObject。
,
public DbSet<Currency> Currencies { get; set; }
告诉EF将其视为实体。
这就是为什么错误信息说Each type name in a schema must be unique. Type name 'Currency' is already defined.
-当需要将货币模型设置为实体时,类型名称货币已经在Movie
实体上定义为复杂类型。
如果你删除一个或其他这些配置行,错误应该消失-但它不会真正解决你的问题,因为你需要考虑-是Currency
在你的领域真正的实体或ValueObject?
Movie
有一个类型为Money
的Price
,它有一个类型为Currency
的属性——这种用法暗示它是一个ValueObject。但是这个类有一个Id
字段——"identity"的存在意味着它是一个实体。
您需要仔细考虑领域建模和在这些类型中体现的业务流程,以探索应该如何真正对待它。