I have a three layer project(DAL, Domain, MVC). I need to bound AuthorEntity from the DAL with ApplicationUser, which is in the MVC project.
They have a different context(EfContext, ApplicationDbContext).
First, I created a UserEntity and tried to implement IUser, which has string field Id (sting Id{get;}), I got an error:
"EntityType 'UserEntity' has no key defined.Define the key for this
EntityType"
I add attributes:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
It's not working. How to do this?
Primary Key property must be readable and writable.
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string Id{get;set;}
Related
I have read in various places, eg
http://techfunda.com/howto/132/primary-key-and-foreign-key-relationship-in-model
...that if you don't call the Id property in your model "Id" or "id" you need to add the annotation [Key], eg
[Key]
public int MyKey { get; set; }
...to it to tell the compiler it's the primary key in the database.
It seems like this may only be a requirement for Code First sites - although I can't find anything conclusive either way.
What is the purpose of [Key] and when should it be used?
Do I need to decorate any primary keys in models that are not called "Id" even when not building a Code First MVC site?
When you run code first and use this:
public class MyClass{
[Key]
public int MyKey { get; set; }
}
This will create in your database:
Table >> dbo.MyClass
Column >> MyKey IDENTITY NOT NULL
When you create a script to create the table, you need to make sure it is a primary key and it is used for code first approach
public class NewClass{
[key]
public int MyKey {get; set;}
}
I use ASP.NET Identity 2.0 in my MVC5 project and wanted to change the data type of id field of AspNetUsers from string (GUID) to int because of that I have some tables related to AspNetUsers table. I look at many pages as on How to change type of id in Microsoft.AspNet.Identity.EntityFramework.IdentityUser but it seems to be not easy to apply. On the other hand, I see that changing the data type of id field of AspNetUsers entity will not be enough as there are some pk/fk relations between the other identity types i.e. AspNetUserRoles, ApplicationGroupRoles, etc.. In that case, should I use the original data type for identity tables and change the id type for my tables from int to string? Or how can I change data type of all of the identity tables easily in ASP.NET Identity 2.0? If this is easier in version 3.0 I can also use it.
I want to extend my comment because, few months ago I was trying to do the same.
In my case, I was not sure if I can create relations string -> int, comming from Database first, and used to create relations between productId(int) and userId(int), I wanted to have int ids all over.
Mate you can let userId like string and create relations like this;
public class Customer
{
public int Id {get;set;}
public string Name {get;set;}
public string SalesManId {get;set;}
[ForeignKey("SalesManId ")]
public virtual ApplicationUser SalesMan {get;set;}
}
Here is the Customer Entity, and I have declared a navigation property!
So, the SalesManId is of type string. In this way you can create all your relations with ApplicationUser class.
If you still want to change to int, here is a youtube tutorial, which I followed few months ago and I manged to change from string to int
YouTube tutorial-How to change UserId from string to int
I am having .NET MVC 5 and Identity...
I am trying to get a one to one relationship for my Member class to my MemberInfo class..
So, My classes looks something like this:
IdentityUser is in the Microsoft.AspNet.Identity.EntityFramework namespace with string Id as its ID.
public class GRNUser : IdentityUser {
....
....
}
public class MemberUser : GRNUser {
public virtual Member MemberInfo {get; set; }
}
public class Member {
public int ID {get; set; }
public string MemberUserID {get; set; }
public virtual MemberUser MemberUser { get; set; }
}
In my Context, I have this
modelBuilder.Entity<Member>().HasRequired(m => m.MemberUser)
.WithOptional(u => u.MemberInfo);
So, the MemberUser and Member should be able to navigate to and from each other using the MemberUser's ID property and Member's MemberUserID property.
However, when my Database is created, it has an additional column "MemberUser_Id" instead of using my MemberUserID that I specified. How do I make it use "MemberUserID" that I specified?
I've tried using a few combination so of the ForiegnKey Data Annotation, but keeps on getting this error:
Member_MemberUser_Source: : Multiplicity is not valid in Role 'Member_MemberUser_Source' in relationship 'Member_MemberUser'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
I don't know whether i understood you right or not, but i'l do my best to help.
(I'l assume that you work with code first migration)
If you want to make a one-to-one relation, why not try to make a standalone entity for your info which will have a foreign key for the user entity and that foreign key will be annotated as the primary key of the entity?
Another way is why just not add whatever attributes you like to the Application User entity and work with it?
In any case i might have misunderstood your purpose, so please feel free to explain further since your post is a bit confusing.
I have created an mvc4 webapi project in VS2012RC and used EF4.3 in my project for Code first.
I used default membership provider. How can i use the UserName as foreign key in another model.
I tried this code ,
[ForeignKey("RegisterModel")]
public string UserName { get; set; }
public virtual RegisterModel User { get; set; }
Is there anything wrong with my code?
Please help
Thanks,
You cannot use UserName because UserName is not primary key in Users table. Only primary keys from principal table can be used as foreign keys in dependent table when using EF because EF doesn't understand unique constraints used in database.
I have this class and table:
public class Foo
{
public Guid Id {get;set;}
public string Name {get;set;}
}
create table Foo
(
id uniqueidentifier primary KEY DEFAULT (newsequentialid()),
name nvarchar(255)
)
the problem is that when i try to save new foo the first one goes with the 0000-000-00 ... id and the second also, so I get constraint exception
anybody knows a fix ?
Have you set Identity StoreGeneratedPattern?
You can do it in the OnModelCreating method:
modelBuilder.Entity<Foo>().Property(o => o.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
or using the DataAnnotation attributes:
public class Foo {
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id {get;set;}
public string Name {get;set;}
}
Just building on Devart's Solution, I had this issue and using the
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
data annotation didn't work. The reason for this was i was using (as suggested in one of the code first tutorials) a SqlServerCompact database which doesn't support the Guid as identity. Just thought I'd post here in case anyone else had this issue. If you change the connection string so it is creating a SqlServer mdf instead of a Compact database it works perfectly.