Hey everyone I am trying to add one to many many to many cascade delete to OnModelCreating but when I add migration it is empty here is the on model creating that I use to have :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
And Here is wha I have updated :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}
DB is already in use so I cannot just wipe it all or re-migrate.And cant backup and build again because there are too many items and project is in use. When I add migration it just looks like this :
public partial class DeleteAll : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
How can I add cascade delete to the project ?
Related
I have been upgrading from .net 4.6 application to .net core 2.1.x and some of these objects and methods are just not available.
Examples:
Issues with :base("name=mydbentities")
OnModelCreating(DbModelBuilder modelBuilder)
base:(nameOrConnectionString)
From the old .net code base are 2 partial classes
public partial class OpsCentralEntities : DbContext
{
public OpsCentralEntities() : base("name=OpsCentralEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
}
In the above code
Error CS1503 Argument 1: cannot convert from 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions'
then this
'OpsCentralEntities.OnModelCreating(DbModelBuilder)': no suitable method found to override
Then with the other partial class
public partial class OpsCentralEntities : IDbContext
{
public OpsCentralEntities(string nameOrConnectionString)
: base(nameOrConnectionString)
{ }
....
}
Cannot convert from 'string' to 'Microsoft.EntityFrameworkCore.DbContextOptions'
On model creating is available
OnModelCreating(DbModelBuilder modelBuilder) -> OnModelCreating(ModelBuilder modelBuilder)
Constructor parameter accept options
base("name=mydbentities")-> YourContextContext(DbContextOptions options) : base(options)
There will be problem if you are using SqlQuery because now you need to add it to model
modelBuilder.Query<YourModel>() //to OnModelCreating(ModelBuilder modelBuilder)
db.Query<YourModel>().FromSql(rawSql)
And last problem i got was many to many should be using Extra type
I'm using SQLite with EFCore, but I got a problem... how can I disable Conventions like Pluralize? Is it possible?
My ModelBuilder has not a property Conventions...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder. [NOT HAS PROPERTY CONVENTION]
}
You can disable pluralizing naming convention as shown below.
public static class ModelBuilderExtensions
{
public static ModelBuilder RemovePluralizingTableNameConvention(this ModelBuilder modelBuilder)
{
foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
{
if (entityType.ClrType == null)
continue;
entityType.Relational().TableName = entityType.ClrType.Name;
}
return modelBuilder;
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.RemovePluralizingTableNameConvention();
}
No! It's not possible in ef core. as #Mustafa told, you can override convention by fluent API configuration.
I am new to Code first Entity framework, when logging into the database after running my app for the first time I got a little confused when I saw the "__MigrationHistory" table.
I now understand the need for this table, but do not like it being in the standard dbo schema within the user table, I think its obtrusive and a risk.
My first thought was to move it to the system folder. When researching how to achieve this within the EF context all I could find is how to move it from system to dbo.
I now get the feeling __MigrationHistory should by default be created within the system folder... is this the case?
How can I configure my context to manage/reference the migration history table within the system folder by default?
Here is my context, am I doing something wrong or missing some configuration?
public class MyContext : DbContext, IDataContext
{
public IDbSet<Entity> Entities { get; set; }
public MyContext()
: base("ConnectionString")
{
}
public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
{
return base.Set<TEntity>();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
There is a technique for moving __MigrationHistory. That table has it's own context (System.Data.Entity.Migrations.History.HistoryContext) that you can override:
public class MyHistoryContext : HistoryContext
{
public MyHistoryContext(DbConnection dbConnection, string defaultSchema)
: base(dbConnection, defaultSchema)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<HistoryRow>().ToTable(tableName: "MigrationHistory", schemaName: "admin");
modelBuilder.Entity<HistoryRow>().Property(p => p.MigrationId).HasColumnName("Migration_ID");
}
}
Then you need to register it:
public class ModelConfiguration : DbConfiguration
{
public ModelConfiguration()
{
this.SetHistoryContext("System.Data.SqlClient",
(connection, defaultSchema) => new MyHistoryContext(connection, defaultSchema));
}
}
You could try executing EXEC sys.sp_MS_marksystemobject __MigrationHistory in your seed method using context.Database.ExecuteSqlCommand();
How do I add all EntityTypeConfiguration<> from current assembly automatically?
public class Entities : DbContext
{
public Entities()
: base("Entities")
{
}
public virtual DbSet<User> Users { get; set; }
// ...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
foreach(var configuration in this.GetAllConfigurations())
{
modelBuilder.Configurations.Add(configuration);
}
}
private ... GetAllConfigurations()
{
// TODO: Get all configurations from current ASSEMBLY
}
}
It should be very easy as DbModelBuilder offers special method for that. Try add this within OnModelCreating method:
modelBuilder.Configurations.AddFromAssembly(typeof(MyDbContext).Assembly);
Simpler Answer
modelBuilder.Configurations.AddFromAssembly(GetType().Assembly);
What is the best way to separate the mapping of tables to entities using the Fluent API so that it is all in a separate class and not inline in the OnModelCreating method?
What I am doing currently:
public class FooContext : DbContext {
// ...
protected override OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<Foo>().Property( ... );
// ...
}
}
What i want:
public class FooContext : DbContext {
// ...
protected override OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.LoadConfiguration(SomeConfigurationBootstrapperClass);
}
}
How do you do this? I am using C#.
You will want to create a class that inherits from the EntityTypeConfiguration class, like so:
public class FooConfiguration : EntityTypeConfiguration<Foo>
{
public FooConfiguration()
{
// Configuration goes here...
}
}
Then you can load the configuration class as part of the context like so:
public class FooContext : DbContext
{
protected override OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new FooConfiguration());
}
}
This article goes into greater detail on using configuration classes.