How can I add a unique contraint to the AspNetUsers.UserName column using Entity Framework migrations? It seems like a jungle to me figuring out how the initial Identity tables are configured.
To make the column unique you can use Data Annotations and the attribute [Index(IsUnique=true)] in your code. Please see this Post by #juFo for more information.
So I managed to merge Identity 2.0 tables in my own database and I also changed the connection string so that now Identity uses my database. I did some test and it works fine.
But how do I change table names?? because I looked over a couple of Identity classes in my project but none of them seems to have any table name inside..
at the moment table names start with AspNet. e.g AspNetUserClames. How do I change it?
I am trying to insert multiple rows using the following code. SaveChanges() works for the first record but if fails when it tries the next one. My guess is that each time it is trying to insert all records. Not sure if this is correct or not, and if it is correct how can I change the code to make it correct. Following is my code:
Contrroller:
foreach(var m in pms)
{
_pmService.AddPms(m);
}
And in _pmService.AddPms:
_pmRepository.Add(pm);
_pmRepository.SaveChanges();
_pmRepository.Add (actually this is from BaseRepository and pmRepository extends this Base)
_ctx.Set<T>().Add(entity);
Finally, here is my SaveChanges() (again this is also from BaseRepostiroy)
_ctx.ChangeTracker.DetectChanges();
return _ctx.SaveChanges();
So in controller, I am looping through each entry, and then calling Add. The Add in service is calls Add in BaseRepository and then it does the SaveChanges(). For first record it works without any issues but 2nd record onwards it fails. And following is the error:
"Saving or accepting changes failed because more than one entity of type XXXXXXXXXXXXX have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption\" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration."
And I am using DatabaseFirst approach.
I found the answer it is with StoreGeneratedPattern. I had to set this Identity in edmx file for the primary key field and it worked.
StoreGeneratedPattern="Identity"
I forced to work with database where tables haven't auto increment. And I can't alter it. I want to insert entity using Entity Framework. I create an object of this entity and manually set it's Id field (primay key) and then make Add and SaveChanges. But I see in log, that EF clear the value of DbParameter for Id field. Is there any solution for this?
You can add an annotation of fluent configuration to tell EF the keys are manual. See Entering keys manually with Entity Framework. You could also add a custom convention to handle globally: Convention for DatabaseGeneratedOption.None
Asp.net mvc 5 Identity 2.0 will create 5 tables automatically when run the mvc project.
Here, my question is why some tables like AspnetUser, it's item 'Id' type defined to string.
The String seems like GUID, but why it doesn't define to guid type instead of using string.
Is it transfer data type from string or do something when quering data ?
I can't figure out why it define to string, but look like guid ?
another table have same problem like AspnetRole, it's item 'UserId', 'RoleId' defined to string too.
Have any idea ?
I'm guessing this is having different reasons.
One reason is the insert performance for Guids, which will get slower and slower after an amount of data (Clustered indexes).
Another reason is the difference of handling Guid in different databases. Microsoft Sql Server has an UniqueIdentifier type that is a Guid, MySql will store them as strings, Oracle stores the raw bytes of a Guid...
I hope this explains a part of your queustion, as not fully :)...
Identity framework not only created for code first approach to be generate database tables with given fields or datatypes. Identity framework can be used against existing database or migrate old asp.net membership provider, or we can use our own table names with extra database fields to store more data on identity tables.
Further more, the Id of the aspnetUser table (that is the User table) used as string because, we can use Guid, integer, long etc. for this field depending on the requirement.
E.g. : If you decided to use integer as Id of the aspnetUser table (User table), then the database field will be auto increment int (or bigInt or etc.) field. But you need to do model binding in order to fulfill entity framework migration requirements.
By default we get Guid inserted into this field when we use out of the box asp.net Identity framework.
When you have defined roles for the registered user, there will be record added into AspnetRole table, this is also completely depending on the fields we defined on the tables as I discussed before. If we decided to use integer as Id of the aspnetUser table, then AspnetRole table fields updated according to the relationship with aspnetUser table fields.
Hope this helps.