I am using En开发者_如何学PythontityFramework 4 with POCO classes, but I like to divide the database up into separate schemas. While I can do this by designing the database first and then generating the model and everything works fine, if I update the model and select to generate the database from the model it ignores all my schemas and generates all tables under the default (or whatever I have set under Database Schema Name).
Is it possible to divide the entities up and have the generate database from model use of those schemas?
Many thanks for any help. I've spent hours on Google and experimenting and I don't think it is possible, but thought I would check.
I don't believe that this is supported in EF4 - as you say, it's a one-way trip only i.e. DB -> code. I don't even think that EFvNext has any plans to do this - how would it work? Based on the namespace in your code?
you can set your shema like this :
public class UserConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.ToTable("User", "MySchema");
builder.Property(a => a.Description).HasMaxLength(2000);
builder.Property(a => a.BirthDate).HasColumnType("date");
}
}
you should call it in your DbContext like this:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(UserConfiguration ).Assembly);
modelBuilder.HasDefaultSchema("MySchema");
base.OnModelCreating(modelBuilder);
}
Without true schema support, EF goes out the window and SQL Database Schemas are being used more and more to improve the database design of databases. Schemas have been in SQL Server 2005 or even earlier.
It is like releasing a new version of .NET but saying it doesn't support javascript. Schemas are a fundamental part of the database development feature set.
I would see it supported as: [Customer].[Staff] supported in the EF as Customer.Staff. So we can have Customer.Staff.StaffID = 10; etc.
精彩评论