Enity Framework migrations - Multiplicity is not valid in Role - c#

I'm going back to EF (Code First) and ATM trying to set up a 1-1 relationship using DataAnnotations.
public class CmsMember
{
[Key]
public int nodeId { get; set; }
public string Email { get; set; }
public string LoginName { get; set; }
public string Password { get; set; }
public Client Client { get; set; }
}
public class Client
{
[ForeignKey("CmsMember")]
public int nodeId { get; set; }
public int ClientId { get; set; }
public string ClientName { get; set; }
public CmsMember CmsMember { get; set; }
}
I'm stuck on error (on add-migration command) saying:
** \tSystem.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'Client_CmsMember_Source' in relationship 'Client_CmsMember'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be ''. *
Any hint would be highly appreciated.

You need key attribute because your property name doesn't match the convention.
[Key, ForeignKey("CmsMember")]
public int nodeId { get; set; }

I think this should help you. There is the same error and your code is similar. You need to change the place of the attribute as well, should be above the property where you want to use in you case above
public CmsMember CmeMember { get; set; }

Related

How to reference two users(each with a different role) in an Entity Framework Class

I have one entity that relates to two users in the AspNetUsers table:
User with the role of Tenant
User with the role of Landlord
Property (Created with entity framework)
I can't figure out how to relate 1 tenant and 1 landlord with 1 property when creating foreign key relationships in my property context. To do this I would need to implement two foreign keys in my property class but they would both be a UserID from Identity. That doesn't seem right though and I can't get my head around it. Below is what the beginning of my Property.cs file would look like.
public class Property
{
[ScaffoldColumn(false)]
public string PropertyID { get; set; }
[Key, ForeignKey("User")]
public string LandlordId { get;set; }
[Key, ForeignKey("User")]
public string TenantId { get;set; }
//other fields
public virtual ApplicationUser User { get;set; }
}
You have two ways to do this:
Keep your current model, and ensure you set up the navigational properties correctly:
public class Property
{
public int PropertyId { get; set; }
public int TenantId { get; set; }
public int LandlordId { get; set; }
public User Tenant { get; set; }
public User Landlord { get; set; }
}
Notice that, since this correctly follows convention over configuration, there's no need for applying [ForeingKey].
This is going to represent a bigger change in your application. You would need to introduce a Landlord and a Tenant entity:
public class Landlord
{
...
public int UserId { get; set; }
public User User { get; set }
}
public class Tenant
{
...
public int UserId { get; set; }
public User User { get; set }
}
And then map those to the Property entity:
public class Property
{
public int PropertyId { get; set; }
public int TenantId { get; set; }
public int LandlordId { get; set; }
public Tenant Tenant { get; set; }
public Landlord Landlord { get; set; }
}
Now, which of those approaches make more sense in your business domain and in this application is up to you to decide.

Can't set primary key and foreign key on one column

So I try to create some ASP.NET project with EF Core.
I want to set propert of one entity as primary key and foreign key to another entity. The relationship is 0..1 - 1. I use DataAnnotations:
public class OfficeAssignment
{
[Key, ForeignKey("InstructorID")]
public int InstructorID { get; set; }
public Instructor Instructor { get; set; }
public string Location { get; set; }
}
But I keep getting column InstructorID as PK and InstructorID1 as FK... Any ideas, why EF behaves like that and how can I achieve my goal?
You should follow convention over configuration as much as you can. An OfficeAssignment entity should have an OfficeAssignmentId PK, like this:
public class OfficeAssignment
{
public int OfficeAssignmentId { get; set; }
//Notice that Id does not have an uppercase D
public int InstructorId { get; set; }
public string Location { get; set; }
public Instructor Instructor { get; set; }
}
However, if you don't want to follow normal conventions, the name of the property that goes in the ForeignKey attribute is the opposite of where it's declared:
public class OfficeAssignment
{
[Key, ForeignKey("Instructor")]
public int InstructorId { get; set; }
public string Location { get; set; }
public Instructor Instructor { get; set; }
}
And, if you want to keep it compile-time safe:
public class OfficeAssignment
{
[Key, ForeignKey(nameof(Instructor))]
public int InstructorId { get; set; }
public string Location { get; set; }
public Instructor Instructor { get; set; }
}
It's enough to set primary key attribute([Key]) in the OfficeAssignment class and in Instructor class we need to set such attribute:
[InverseProperty("Instructor")]
on collection of CourseAssignments. That will work as desired.

Foreign key invalid with Entity Framework

So i am trying to create a table with a foreign key, but it always says that it cannot find the foreign key. heres the code:
public class Tecnologies
{
[Key]
public int TecId { get; set; }
[Required]
public String Name { get; set; }
}
this one works, then i try to create this one:
public class UserTecnologies
{
[Key]
public int UserTecId { get; set; }
[ForeignKey("Id")]
public UserProfile User { get; set; }
[ForeignKey("TecId")]
public virtual Tecnologies Tecnology { get; set; }
[Required]
public int Rating { get; set; }
}
and it gives me the error :
The ForeignKeyAttribute on property 'Tecnology' on type 'ESW_CloddOffice.Models.UserTecnologies' is not valid. The foreign key name 'TecId' was not found on the dependent type 'ESW_CloddOffice.Models.UserTecnologies'. The Name value should be a comma separated list of foreign key property names.
The names are correct, what am i missing ?
Okay, i found what i was doing wrong. Heres the correct code:
public class UserTecnologies
{
[Key]
public int UserTecId { get; set; }
[ForeignKey("UserProfile")]
public virtual int UserProfileId { get; set; }
[ForeignKey("Tecnology")]
public virtual int TecnologyId { get; set; }
public virtual Tecnologies Tecnology { get; set; }
public virtual UserProfile UserProfile { get; set; }
[Required]
public int Rating { get; set; }
}
Was creating the foreign key the wrong way .
The ForeignKey attribute requires that an actual property on the entity match the name you pass in. It doesn't just tell EF what to call the key at the database level.
You either need to actually add a TecId property:
public int TecId { get; set; }
Or use fluent configuration, instead:
modelBuilder.Entity<UserTechnologies>()
.HasRequired(c => c.Technology)
.WithMany()
.Map(m => m.MapKey("TecId"));

Entity Framework Two foreign keys as primary key

So i am working with entity code firest and i have a user class that looks like this:
public class User
{
[Key]
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime LastModified { get; set; }
}
I am trying to make a "friends table" and no matter what i come up with I get an error on the db creation. This is what I Currently have in the Friends Class:
public class Friend
{
[Key, ForeignKey("User")]
public virtual User MyUser { get; set; }
[Key,ForeignKey("User")]
public virtual User MyFriend { get; set; }
public bool IsAccepted { get; set; }
}
this is the error i get:
The ForeignKeyAttribute on property 'MyUser' on type 'Core.Model.Friend' is not valid. The foreign key name 'User' was not found on the dependent type 'Core.Model.Friend'. The Name value should be a comma separated list of foreign key property names.
What am I missing?
You need to use the Column attribute. Normally I would use something like this:
public class Friend
{
[Key]
[Column(Order = 0)]
public int MyUserId { get; set; }
[Key]
[Column(Order = 1)]
public int MyFriendId { get; set; }
[ForeignKey("MyUserId")]
public virtual User MyUser { get; set; }
[ForeignKey("FriendId")]
public virtual User MyFriend { get; set; }
public bool IsAccepted { get; set; }
}
I'm not sure what would happen if you map the Column attribute directly to the navigation property. You can try it if you like and see what happens.. but the above generally works for me.
Alternatively, if you change to use fluent mapping, you can do something like this:
HasKey(u => new { u.MyUserId , u.MyFriendId });

One entity to another with multiple properties

I have a problem with the table creation of EF5 Code first. I'm going to demonstrate two cases and their outputs. What I want to know what the difference between them and how I could be successful on my goal.
Case 1
public class User{
public int ID { get; set; }
public string Name { get; set; }
public ICollection<UserMessage> Messages { get; set; }
}
public class UserMessage{
public int ID { get; set; }
public string Message { get; set; }
public User CreatedBy { get; set; }
public ICollection<User> PermittedUsers {get; set;}
}
Now in this case, entity framework generates only User and UserMessage classes that are only binding to each other with UserMessage_ID in User and CreatedBy_ID in UserMessage.
The problem that I'm curious is in here. How will EF5 recognize permitted users whenever I create a new message? Is it going to duplicate the same message for each permitted users? Or what?
Case 2
public class User{
public int ID { get; set; }
public string Name { get; set; }
public ICollection<UserMessage> Messages { get; set; }
}
public class UserMessage{
public int ID { get; set; }
public string Message { get; set; }
public ICollection<User> PermittedUsers {get; set;}
}
In this case I have removed just only CreatedBy in UserMessage entity. Now EF5 generates 3 tables named : User, UserMessage, UserMessageUsers. The links between UserMessage and User is now over UserMessageUsers so that we can easily manage permitted users.
How can I add CreatedBy property into UserMessage without breaking up Case 2 ?
You can use fluent api to configurate it:
modelBuilder.Entity<UserMessage>()
.HasMany(d=>d.PermittedUsers)
.WithMany(d=>d.Messages)
.Map(c=>c.ToTable("UserMessageUsers"));

Categories

Resources