I'm currently creating my first "complex" application with Entity Framework, and I'm encountering my first problem.
I have two entities: Users and Events, a defined User can either organize an Event or be a participant in an Event. I would like to implements relationship between these entities in a manner that, for a defined user, I can either retrieve all the events organized by him or retrieve all the events he subscribed for.
Event.cs
public virtual User Organizer { get; set; }
public List<User> Participants { get; set; }
User.cs
public virtual ICollection<Event> EventsOrganized { get; set; }
public virtual ICollection<Event> EventsSubscribedFor { get; set; }
How can I specify that the EventsOrganized should refer to the Organizer property, and the EventsSubscribedFor should refer to the Participants property?
I am going to assume you can you Fluent API.
In your DbContext class create or add to your OnModelCreating override
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasMany(s => s.EventsOrganized)
.WithRequired(c => c.Organizer)
.WillCascadeOnDelete(false);
modelBuilder.Entity<User>()
.HasMany(s => s.EventsSubscribedFor)
.WithMany(c => c.Participants)
.Map(cs =>
{
cs.MapLeftKey("UserId");
cs.MapRightKey("EventId");
});
base.OnModelCreating(modelBuilder);
}
What is going on is I am telling the DbContext that the User Entity has many EventOrganized with a required Organizer, and then telling the DbContext to not cascade deletes.
Then I am telling the DbContext that the User Entity has many EventsSubscribedFor to Many Participants. Then I map the left and right keys. This creates a table called "UserEvents", you could name it something else by saying cs.ToTable("NameOfTable");
Also For reference EntityframeworkTutorials helped me learn about the Fluent API, and these are the entities I used to test.
public class User
{
public User()
{
EventsOrganized = new HashSet<Event>();
EventsSubscribedFor = new HashSet<Event>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Event> EventsOrganized { get; set; }
public virtual ICollection<Event> EventsSubscribedFor { get; set; }
}
public class Event
{
public Event()
{
Participants = new HashSet<User>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int OrganizerId { get; set; }
public virtual User Organizer { get; set; }
public ICollection<User> Participants { get; set; }
}
Related
I have the following entities
public class Course
{
public long Id { get; set; }
public virtual ICollection<User> Users{ get; set; }
public virtual ICollection<UserCourse> CourseUsers { get; set; }
}
public class User
{
public long Id { get; set; }
public virtual ICollection<Course> Courses { get; set; }
public virtual ICollection<UserCourse> UserCourses { get; set; }
}
public class UserCourse
{
public long UserId { get; set; }
public User User { get; set; }
public long CourseId { get; set; }
public Course Course { get; set; }
public bool IsRequired { get; set; }
}
with the following mappings for
UserCourse mapping :
builder
.HasOne(nav => nav.User)
.WithMany(self => self.UserCourses)
.HasForeignKey(fk => fk.UserId)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasOne(nav => nav.Course)
.WithMany(self => self.CourseUsers)
.HasForeignKey(fk => fk.CourseId)
.OnDelete(DeleteBehavior.Cascade);
and the User mapping
builder
.HasMany(nav => nav.Courses)
.WithMany(nav => nav.Users);
When trying to create a new migration I'm not exactly sure why I'm getting this.
Cannot use table 'UserCourse' for entity type 'UserCourse' since it is
being used for entity type 'UserCourse(Dictionary<string, object>)'
and potentially other entity types, but there is no linking
relationship. Add a foreign key to 'UserCourse' on the primary key
properties and pointing to the primary key on another entity typed
mapped to 'UserCourse'.
I understand what the error is, but not sure how to force the UserCourse mapping to use the User mapping generated join table or vice-versa
Also, I need the direcat mapping for OData, and the indirect mapping using the join entity to conduct operations on DbSet<UserCourse>
The public virtual ICollection<User> Users{ get; set; } in Course entity and the the public virtual ICollection<Course> Courses { get; set; } in Users entity are redundant. The entities should look more like this
public class Course
{
public long Id { get; set; }
public virtual ICollection<UserCourse> UserCourses { get; set; }
}
public class User
{
public long Id { get; set; }
public virtual ICollection<UserCourse> UserCourses { get; set; }
}
public class UserCourse
{
public long UserId { get; set; }
public User User { get; set; }
public long CourseId { get; set; }
public Course Course { get; set; }
}
And the OnModelCreating method should have this code
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserCourse>()
.HasKey(uc => new { uc.UserId, uc.CourseId });
modelBuilder.Entity<UserCourse>()
.HasOne(uc => uc.Course)
.WithMany(c => c.Users)
.HasForeignKey(uc => uc.CourseId);
modelBuilder.Entity<UserCourse>()
.HasOne(uc => uc.User)
.WithMany(c => c.Courses)
.HasForeignKey(uc => uc.UserId);
}
If you use EF core 5 you can directly skip the join table. It will be generated and handled by EF behind the scenes. More on the topic here https://www.thereformedprogrammer.net/updating-many-to-many-relationships-in-ef-core-5-and-above/
I'm trying to make an insert in a SQL database using Entity Framework 6 and I'm stuck on this issue that I cannot solve.
The error that I keep getting is :
UpdateException: Entities in 'Connect.CompanyFinancialDetails' participate in the 'Company_CompanyFinancialDetails' relationship. 0 related 'Company_CompanyFinancialDetails_Source' were found. 1 'Company_CompanyFinancialDetails_Source' is expected
I have these 2 entities:
public class Company
{
public long CUI { get; set; }
public string UserName { get; set; }
public string CompanyName { get; set; }
public string Symbol { get; set; }
public int? SharesCount { get; set; }
public decimal? SharePrice { get; set; }
public virtual Account Account { get; set; }
public virtual CompanyFinancialDetails CompanyFinancialDetails { get; set; }
}
public class CompanyFinancialDetails
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
// other properties
public decimal? NumberOfEmployees { get; set; }
public virtual Company Company { get; set; }
}
This is the Fluent API configuration:
public DbSet<Account> SignUpModels { get; set; }
public DbSet<Company> Companies { get; set; }
public DbSet<CompanyFinancialDetails> CompanyFinancialDetails { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Account>()
.HasKey(k => k.Id)
.HasOptional(s => s.Company)
.WithRequired(d => d.Account);
modelBuilder.Entity<Company>()
.HasKey(k => k.CUI)
.HasOptional(s => s.CompanyFinancialDetails)
.WithRequired(d => d.Company);
}
The relationship that I want to have is 1-many (one Company has many CompanyFinancialDetails).
This is the code where I add the objects to the database:
Company co = Context.Find(username);
foreach (CompanyFinancialDetails s in c)
{
s.Company = co;
}
a.CompanyFinancialDetails.AddRange(c);
a.SaveChanges();
I get a list of CompanyFinancialDetails and I add them using the AddRange method. I had this issue before and what I did was to add the virtual property object to the object that I wanted to insert in the database and it worked. This is what I tried to do here: the Find() method gets the company object that is related to the CompanyFinancialDetails and for each CompanyFinancialDetails object an Company virtual property is adding the related company object.
Well, it didn't work, when the SaveChanges() method is called, I get that error. Any help would be appreciated.
I have 2 model classes for Users and Organizations.
public class User : IdentityUser
{
[Required]
public string Name { get; set; }
[Required]
public string Surname { get; set; }
public int? OrganizationID { get; set; }
public virtual OrgList org { get; set; }
}
public class OrgList
{
public OrgList()
{
employees = new HashSet<User>();
}
public int id { get; set; }
public String name { get; set; }
public String ownerId { get; set; }
public virtual ICollection<User> employees { get; set; }
public virtual User ownerUser { get; set; }
}
User can be owner of some organization and also he is employee of the same organization (But other employees can't be owners of the organization).
First i've created a relationship for employees and it works OK
modelBuilder.Entity<OrgList>(entity =>
{
entity.HasMany(e => e.employees)
.WithOne(e => e.org)
.HasForeignKey(e => e.OrganizationID)
.OnDelete(DeleteBehavior.SetNull);
}
but when i try to add another relationship for owner
entity.HasOne(e => e.ownerUser)
.WithOne(e => e.org)
.HasForeignKey<OrgList>(e => e.ownerId)
.OnDelete(DeleteBehavior.Cascade);
i have an error on migration:
Cannot create a relationship between 'User.org' and
'OrgList.ownerUser', because there already is a relationship between
'OrgList.employees' and 'User.org'. Navigation properties can only
participate in a single relationship.
How can i fix it? I've found an answers for EF6 (not EF Core) with HasOptional() and WithOptionalPrincipal() methods that not exist in EF Core.
Can i do it without creating additional table for employees or without creating additional virtual OrgList on User class?
You're trying to create the owner relationship with the same property on the user that you are using for the employee relationship. Entity framework wouldn't know which relationship to assign the property. If you created another property on the user like
public int? OwnedOrganizationID { get; set; }
public virtual OrgList OwnedOrg { get; set; }
and change the statement to
entity.HasOne(e => e.ownerUser)
.WithOne(e => e.OwnedOrg)
.HasForeignKey<OrgList>(e => e.ownerId)
.OnDelete(DeleteBehavior.Cascade);
I imagine it should work.
I'm trying to make a enitity that manages membership of a user in a organization with a role. I want to restrict a user to have only one membership in an organization. I'm doing this by creating a composite key. However i get the error when i try to create the initial migrations:
InvalidOperationException: The property 'User' cannot be added to the entity type 'OrganizationLogin' because a navigation property with the same name already exists on entity type 'OrganizationLogin'.
The entity for membership
public class OrganizationLogin
{
public int OrganizationLoginId { get; set; }
public OrganizationRole Role { get; set; }
public Organization Organization { get; set; }
public OmegaUser User { get; set; }
}
My DBContext where I try to define the composite key:
public class OmegaContext : IdentityDbContext<OmegaUser,OmegaRole,int>
{
public DbSet<Log> Logs { get; set; }
public DbSet<Organization> Organizations { get; set; }
public DbSet<OrganizationLogin> OrganizationLogins { get; set; }
public DbSet<OrganizationRole> OrganizationRoles { get; set; }
public OmegaContext()
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<OrganizationLogin>(orgLogin =>
{
orgLogin.HasAlternateKey(o => new {o.User, o.Organization});
});
}
}
If i remove the OnModelCreating code, the migrations are created succesfully.
EDIT: As mentioned in the comments, the problem was that i was referencing the class and not a property that had the key of the entities
As requested, here is my solution:
public class OrganizationUnitMember
{
public int OrganizationUnitMemberId { get; set; }
public int UserId { get; set; }
public int OrganizationUnitId { get; set; }
[ForeignKey("UserId")]
public virtual OmegaUser User { get; set; }
[ForeignKey("OrganizationUnitId")]
public virtual OrganizationUnit OrganizationUnit { get; set; }
public int RoleId { get; set; }
[ForeignKey("RoleId")]
public virtual OrganizationRole Role { get; set; }
}
And the DbContext:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<OrganizationUnit>(
orgUnit =>
{
orgUnit.HasOne(ou => ou.Parent)
.WithMany(ou => ou.Children)
.OnDelete(DeleteBehavior.Restrict)
.HasForeignKey(ou => ou.ParentId);
});
builder.Entity<OrganizationUnitMember>(member =>
{
member.HasAlternateKey(m => new {m.OrganizationUnitId, m.UserId});
});
}
I had to add the ids of the referenced entities
I have the following entities:
public class User
{
[Key]
public int UserId { get; set; }
virtual public ICollection<Transaction> Transactions { get; set; }
}
public class Transaction
{
[Key]
public Int64 TransactionId { get; set; }
[Required]
public virtual User Sender {get; set;}
public virtual User Receiver { get; set; }
}
The relationship is described in fluent API
modelBuilder.Entity<User>()
.HasMany(r => r.Transactions)
.WithRequired(s => s.Sender);
modelBuilder.Entity<User>()
.HasMany(r => r.Transactions)
.WithOptional(r => r.Receiver);
There are two users, one is a sender of the transaction the second is the receive.
Now when I add a transaction to the first user everything works. When the second user accepts the transaction and I add the same transaction to its ICollection of transactions it magically disappears from the first user and vise versa. In other words EF prevents me from referencing the same entity in both parents. Is there way around it ?
You need to have two navigation properties in your User Class.
public class User
{
[Key]
public int UserId { get; set; }
virtual public ICollection<Transaction> SenderTransactions { get; set; }
virtual public ICollection<Transaction> ReceiverTransactions { get; set; }
}
Each navigation property must have equivalent navigation property in related class.
modelBuilder.Entity<User>()
.HasMany(r => r.SenderTransactions)
.WithRequired(s => s.Sender);
modelBuilder.Entity<User>()
.HasMany(r => r.ReceiverTransactions)
.WithOptional(r => r.Receiver);