c# EF generating wrong table names on SQL Compact - c#

Using latest EF and EF SQL Compact (6.1.1) with SQL (4.0)....
Noticed via Linqpad that the table names generated through my initializer (DropCreateDatabaseAlways) are different than what I have specified in the Table attribute. When developing I am creating an embedded database for test purposes but other environment the database will be legacy which means the table names must match then.
If I have [Table("p_Brand_Visited")] on an entity the generated table name is P_Brand_Visiteds. The first letter is capitalized and the name is made plural. Not sure why the Table attribute is not respected. Maybe a SqlCe issue?
Tried via the OnModelCreating event to remove pluralizing:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
No effect. Tried to set the table name:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<BrandVisited>().ToTable("p_Brand_Visited");
}
No effect. Any other suggestions? Ways to debug?

This was a problem with Linqpad and the plural options. Once these were disabled everything worked fine.

Related

EF6 dynamically changing a Schema

I have a database that contains an unknown number of schemas, each schema has the exact same tables.
Is there anyway to change the schema at runtime when creating a new instance dbContext? I know it's possible to change connection strings at runtime, however the schema names is stored in another database as a table of configurations.
I saw that in EF6 you can set a defaultSchema when overriding OnModelCreating however I am not too sure if this is guaranteed to run everytime I use using (DatabaseContext db = new DatabaseContext()){}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema(SchemaName);
}

How to change table name using database first Entity Framework

This is similar to this question but with using database first entity framework instead of code first:
How can I change the table names when using Visual Studio 2013 ASP.NET Identity?
In the code first approach you can override OnModelCreating and do something like this so that the user info is saved in MyUsers table instead of the default AspNetUsers:
modelBuilder.Entity<IdentityUser>().ToTable("MyUsers")
In the database first approach I have manually created the MyUsers table but because OnModelCreating does not get called I don't know how to configure the data to be saved into my new table?
You can follow the Code First approach an when overriding the OnModelCreating add the line:
System.Data.Entity.Database.SetInitializer<ApplicationDbContext>(null);
The line above will link the AspNetIdentity entities to your tables without re-creating the tables.
Code Example:
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUserRole>().ToTable("UserRoles");
modelBuilder.Entity<ApplicationUserLogin>().ToTable("UserLogins");
modelBuilder.Entity<ApplicationUserClaim>().ToTable("UserClaims");
modelBuilder.Entity<ApplicationRole>().ToTable("Roles");
System.Data.Entity.Database.SetInitializer<ApplicationDbContext>(null);
}

Entity Framework 5 rc Code First Specifying Schema to create tables under?

Using EF 5 RC what is the proper way to specify what schema tables are created under while using the Code First process?
I have tried:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Metric>().ToTable("Metrics", "Dashboard");
}
But the SQL script created doesn't recognize the schema and makes all the tables under the dbo schema.
TIA
J
If you right click on the EDMX designer surface and go to properties you can set the schema for that diagram. So if you want to put different entities/tables in different schemas you have to keep them on seperate design surfaces.

Is there a way to force all property name maps to use ALL CAPS in EF 4.3 Code-First?

I'm using EF 4.3 code-first with an Oracle database. One weird thing about the database is that it automatically makes table and column names ALL CAPS. My domain properties are in PascalCase. Unfortunately, EF doesn't know to capitalize column names, so my queries don't work. I have hundreds of properties on several classes that should map to oracle columns. I would really rather not manually code all those mappings. Is there a way to use a convention to capitalize column names?
SUMMARY:
I need for Entity Framework to use ALL CAPS for all column names. How can I do that?
Haven't tried this, but what about something like this:
public class SomeEntities : DbContext
{
public DbSet<Entity> Entities { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Configure Code First to ignore ColumnTypeCasing convention
modelBuilder.Conventions.Remove<ColumnTypeCasingConvention>();
}
}

EF 4 Code First: How can I modify naming conventions for generated junction tables?

I'm integrating with an existing database, so I would like the tables generated by my EF4 Code First code to adhere to the table naming conventions which are already in place.
The existing convention is the singular entity name, prefixed with a lowercase "t" (e.g tProduct), and joined with an underscore for junction tables (e.g tProduct_tOffer).
I've achieved this with all the 'standard' tables using the code below. In my DbContext I have:
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
modelBuilder.Conventions.Add<TableNamePrefixConvention>();
base.OnModelCreating(modelBuilder);
}
And the following class adds the "t" to table names:
public class TableNamePrefixConvention : IConfigurationConvention<Type, EntityTypeConfiguration> {
public void Apply(Type typeInfo, Func<EntityTypeConfiguration> configuration) {
configuration().ToTable("t" + typeInfo.Name);
}
}
However, when a junction table is generated, it doesn't adhere to these conventions. So I now have this kind of thing in the list of tables:
tOffer
tProduct
ProductOffers
In this case, ProductOffers should be named tProduct_tOffer. How can I force the generated junction table to conform to my naming style?
Update: as Ladislav correctly guessed below, I am using CTP5.
As I pointed in the comment custom conventions are not available in RC version and will not be available in RTW as well (official announcement).
You must map it manually:
modelBuilder.Entity<Offer>()
.HasMany(o => o.Products)
.WithMany(p => p.Offers)
.Map(m => m.ToTable("tProduct_Offer"));
The code snippet is RC version. CTP5 used one more step with IsIndependent() but it was removed in RC.

Categories

Resources