Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4 Generate Database From Model With Multiple Schemas

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.

like image 552
user351711 Avatar asked Dec 05 '25 04:12

user351711


2 Answers

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?

like image 110
Isaac Abraham Avatar answered Dec 07 '25 18:12

Isaac Abraham


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);
        }
like image 33
hussein zakizadeh Avatar answered Dec 07 '25 19:12

hussein zakizadeh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!