How to remove child one to many related records in EF code first database?
In EF6 a faster way to do the operation is… context.Children.RemoveRange(parent.Children)
In EF6 a faster way to do the operation is… context.Children.RemoveRange(parent.Children)
Remove your Essence2EssenceSet model class. If junction table contains only keys of related entities participating in many-to-many relations it is not needed to map it as entity. Also make sure that your fluent mapping of many-to-many relations specifies schema for table: mb.Entity<Essence>() .HasMany(e => e.EssenceSets) .WithMany(set => set.Essences) .Map(mc => { mc.ToTable(“Essence2EssenceSet”, “Com”); mc.MapLeftKey(“EssenceID”); mc.MapRightKey(“EssenceSetID”); … Read more
You can implement a custom sql generator class derived from SqlServerMigrationSqlGenerator from System.Data.Entity.SqlServer namespace: public class CustomSqlGenerator : SqlServerMigrationSqlGenerator { protected override void Generate(AddForeignKeyOperation addForeignKeyOperation) { addForeignKeyOperation.Name = getFkName(addForeignKeyOperation.PrincipalTable, addForeignKeyOperation.DependentTable, addForeignKeyOperation.DependentColumns.ToArray()); base.Generate(addForeignKeyOperation); } protected override void Generate(DropForeignKeyOperation dropForeignKeyOperation) { dropForeignKeyOperation.Name = getFkName(dropForeignKeyOperation.PrincipalTable, dropForeignKeyOperation.DependentTable, dropForeignKeyOperation.DependentColumns.ToArray()); base.Generate(dropForeignKeyOperation); } private static string getFkName(string primaryKeyTable, string foreignKeyTable, params string[] … Read more
I finally found an answer for this, by writing a custom convention. This convention works in EF 6.0 RC1 (code from last week), so I think it’s likely to continue to work after EF 6.0 is released. With this approach, the standard EF conventions identify the independent associations (IAs), and then create the EdmProperty for … Read more
Just omit the [Required] attribute from the string somefield property. This will make it create a NULLable column in the db. To make int types allow NULLs in the database, they must be declared as nullable ints in the model: // an int can never be null, so it will be created as NOT NULL … Read more
Use DropCreateDatabaseAlways initializer for your database. It will always recreate database during first usage of context in app domain: Database.SetInitializer(new DropCreateDatabaseAlways<YourContextName>()); Actually if you want to seed your database, then create your own initializer, which will be inherited from DropCreateDatabaseAlways: public class MyInitializer : DropCreateDatabaseAlways<YourContextName> { protected override void Seed(MagnateContext context) { // seed database … Read more
If you don’t want to use migrations but in the same time you want EF to create database for you, you just need to set correct database initializer: Database.SetInitializer<YourContextType>(new CreateDatabaseIfNotExists<YourContentType>());
Try to use ColumnAttribute from System.ComponentModel.DataAnnotations (defined in EntityFramework.dll): [Column(TypeName=”Date”)] public DateTime ReportDate { get; set; }
The database will only be created when you actually use the context. If you have overridden the Seed method in your initializer as follows: protected override void Seed(MyContext context){…} The Seed code will only run when you use an instance of MyContext. That is why it works when you use var ctx = new MyContext(); … Read more
I believe you always need navigation property on at least one side when using code-first. Then you will be able to map it: public class User { public string UserId { get; set; } public string PasswordHash { get; set; } public bool IsDisabled { get; set; } public DateTime AccessExpiryDate { get; set; } … Read more