Entity Framework: Add Unique Constraint to existing property - entity-framework

I have an existing model, SomeModel, which contains a property Prop. I'd like to enforce that the values of Prop be unique.
To this end, I'm adding the following to my model:
public static void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SomeModel>()
.HasAlternateKey(a => a.Prop);
}
but ef migrations isn't picking up the change.
How can I correct this?

This works for me with EF Core on an already existing model. add-migration picks up the change.
public static void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SomeModel>()
.HasIndex(u => u.Prop)
.IsUnique();
}

Related

ignore one table with ef-core migration

I have an ASP.NET Core application. I have structured the application as multiple projects under the solution. In two of the projects I have 2 different contexts for the same database. The problem is I have a table I am using for auditing in both contexts, and this is causing a problem with migration.
My question is:
is there anyway I can make migration ignore creating this table in one of the contexts?
I am getting the error in the following line:
dbContext.Database.Migrate();
in you dbContext you can ignore one or more table using model builder ignore and give the entity class type you want to ignore
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Ignore<YourClassHere>();
}
You can do this by adding onModelCreating method.
public class ApplicationDbContext : DbContext
{
public DbSet<TableName> TableNames { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TableName>().ToTable(nameof(TableNames), t =>
t.ExcludeFromMigrations());
}
}

code first cascade delete in config

Is there anyway to cascade delete false in the entity framework config settings. For now I have done using fluent API but there are multiple place where cascade delete need to be set false, so is there any single point where I can set for all classes, as I don't have any need of it.
Thanks.
public partial class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//Set cascade options here also
}

Cannot find plural form of table

For some reason code-first EF7 (vNext) will not use/find the plural form of my table. I have tried adding the table attribute to the model but it does not solve the problem.
[Table("Units")]
public class Unit
If I name the table Unit then no problem. If I name the table Units then it's not found.
What am I doing wrong or missing?
Thank you.
This is how I resolved:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Unit>().ToTable("Units");
}
For Entity Framework 7 beta1, I solved this issue by this way:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Unit>().ForRelational(rb =>
{
rb.Table("Units");
});
}
Entity Framework 7 is being configured using the Fluent API. I created an extension method which maps the table names to their plural form, with the intention to reproduce the EF 6 behavior and to be able to use my existing database while working with EF7.
public static class ModelBuilderExtensions
{
public static void PluralizeNames(this ModelBuilder modelBuilder)
{
var types = modelBuilder.Model.EntityTypes;
foreach (var type in types.Where(type => type.ClrType != null))
{
modelBuilder.Entity(type.ClrType)
.ForRelational()
.Table(type.ClrType.Name.Split('`')[0].Pluralize());
};
}
}
Please note the .Pluralize() extension method. This may be Humanizer you're using or any other extension method which pluralizes your string. (I shamelessly copied https://github.com/srkirkland/Inflector/blob/master/Inflector/Inflector.cs in my project to be able to compile my project with the DNX Core.)
The .Split() part is to deal with the type.ClrType.Name which can output stuff like IdentityUserRole`1.
You can use it like this in your DbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.PluralizeNames();
}
Ps; this works for me
Now the ToTable and ForRelational both are missing in beta5 of EF7. So I used the below code.
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<Role>().ForSqlServer().Table("Role");
}
You need to add "Microsoft.EntityFrameworkCore.Relational" in your project.json and restore your package.
.NET core is broken down in to small pieces for less memory foot print. So you need to explicitly tell what you want.

Convention for DatabaseGeneratedOption.None

I would like to manually insert id for all my entities.
Is it possible to create some kind of convention or I have to set HasDatabaseGeneratedOption(DatabaseGeneratedOption.None) (or add DatabaseGenerated attribute) for every entity ?
EDIT
There is a simpler way to do this then using accepted answer:
modelBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>();
It's possible to create custom conventions in EntityFramework 6 as follows:
Create a convention class
public class ManualIdentityConvention : Convention
{
public ManualIdentityConvention()
{
this.Properties<int>()
.Where(p => p.Name.EndsWith("Id"))
.Configure(p => p.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None));
}
}
Add the convention to your DbContext
public class Context : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new ManualIdentityConvention());
}
}
EntityFramework 5
As for EntityFramework 5, I believe something similar can be achieved, but not via a convention class:
public class Context : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Properties<int>()
.Where(x => x.Name.EndsWith("Id"))
.Configure(x => x.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None));
}
}
In either case, neither approach is particularly surgical, but one could conceivably tighten up the convention by being more specific in one's where clause.
Does this help?

How do I singularize my tables in EF Code First?

I prefer using singular nouns when naming my database tables. In EF code first however, the generated tables always are plural. My DbSets are pluralized which I believe is where EF is generating the names but I don't want to singularize these names as I believe it is more pratical to have them plural in code. I also tried overriding the setting but to no avail.
Any ideas? Here is my code and thanks.
MyObjectContext.cs
public class MyObjectContext : DbContext, IDbContext
{
public MyObjectContext(string connString) : base(connString)
{
}
public DbSet<Product> Products {get;set;}
public DbSet<Category> Categories {get;set;}
//etc.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
}
}
You've removed the wrong convention (PluralizingEntitySetNameConvention) for this purpose. Just replace your OnModelCreating method with the below and you will be good to go.
using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db;
...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
With Entity Framework 6, on your file that inherit from DbContext:
using System.Data.Entity.ModelConfiguration.Conventions;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
You can also change the property value:
On the Tools menu, click Options.
In the Options dialog box, expand Database Tools.
Click O/R Designer.
Set Pluralization of names to Enabled = False to set the O/R Designer so that it does not change class names.
Set Pluralization of names to Enabled = True to apply pluralization rules to the class names of objects added to the O/R Designer.
The location of the definition of PluralizingTableNameConvention has moved to:
using System.Data.Entity.ModelConfiguration.Conventions;