I am using EntityFramework 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With