ManyToMany Database With Three Models - c#

I have more of a theoretical question. I have three Models. Employee, Commission and Position. Currently I created a ManyToMany relationship database EmployeeCommission (an Employee has many Commissions and Commissions have many Employees) but I also need to add Position to the relationship which would make that Commissions have many Employees that can have many Positions and so on. (Same employee can have different positions in different commissions).
How do I go about this? I know that ManyToMany relationship can only be between to models, so how do I create a ManyToMany database between three models?
Here's my Models, but I don't think you'll need them, but just for general information.
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
[Display(Name = "Phone Number")]
public int PhoneNumber { get; set; }
}
public class Commission
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Position
{
public int Id { get; set;}
public string Name { get; set; }
}
Note: I am using CodeFirst approach.

You can View thisexample to do that:
public class User
{
public int Id { get; set; }
public ICollection<UserAccount> UserAccounts { get; set; }
}
public class Account
{
public int Id { get; set; }
public ICollection<UserAccount> UserAccounts { get; set; }
}
public class UserAccount
{
public int UserId { get; set; }
public int AccountId { get; set; }
public int RoleId { get; set; }
public User User { get; set; }
public Account Account { get; set; }
public Role Role { get; set; }
}
public class Role
{
public int Id { get; set; }
public string RoleName { get; set; }
public ICollection<UserAccount> UserAccounts { get; set; }
}
Configuration:
modelBuilder.Entity<UserAccount>()
.HasKey(e => new { e.UserId, e.AccountId });
modelBuilder.Entity<UserAccount>()
.HasRequired(e => e.User)
.WithMany(e => e.UserAccounts)
.HasForeignKey(e => e.UserId);
modelBuilder.Entity<UserAccount>()
.HasRequired(e => e.Account)
.WithMany(e => e.UserAccounts)
.HasForeignKey(e => e.AccountId);
modelBuilder.Entity<UserAccount>()
.HasRequired(e => e.Role)
.WithMany(e => e.UserAccounts)
.HasForeignKey(e => e.RoleId);
You can create a new UserAccount in a several ways.

Related

Unable to determine the relationship represented by navigation [duplicate]

I have my entities like this, they are closely linked.
public class Game
{
public int Id { get; set; }
public int FirstTeamId { get; set; }
public Team FirstTeam { get; set; }
public int SecondTeamId { get; set; }
public Team SecondTeam { get; set; }
public Stadium Stadium { get; set; }
public DateTime Date { get; set; }
public GameStatus Result { get; set; }
public Game(DateTime date , GameStatus result )
{
Date = date;
Result = result;
}
}
public class Player
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public DateTime Birthday { get; set; }
public PlayerStatus Status { get; set; }
public PlayerHealthStatus HealthStatus { get; set; }
public int Salary { get; set; }
public int TeamId { get; set; }
public Team Team { get; set; }
public Player(string name , string surname, DateTime birthday, PlayerStatus status, PlayerHealthStatus healthStatus, int salary)
{
Name = name;
Surname = surname;
Birthday = birthday;
Status = status;
HealthStatus = healthStatus;
Salary = salary;
}
}
public class Stadium
{
public int Id { get; set; }
public string Name { get; set; }
public int Capacity { get; set; }
public int PriceForPlace { get; set; }
public Stadium(string name, int capacity, int priceForPlace)
{
Name = name;
Capacity = capacity;
PriceForPlace = priceForPlace;
}
}
public class Team
{
public int Id { get; set; }
public string Name { get; set; }
public List<Player> Players { get; set; }
public List<Game> Games { get; set; }
public Team(string name)
{
Name = name;
}
public Team(string name, List<Player> players) : this(name)
{
Players = players;
}
}
In my Context class I'm tried to describe my relationships between classes. But something isn't correct.
public class ApplicationContext : DbContext
{
public DbSet<Player> Players { get; set; }
public DbSet<Game> Games { get; set; }
public DbSet<Team> Teams { get; set; }
public DbSet<Stadium> Stadiums { get; set; }
public ApplicationContext()
{
Database.EnsureCreated();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=best-komp;Database=FootballApplicationDataBase;Trusted_Connection=True;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Player>()
.HasOne(p => p.Team)
.WithMany(t => t.Players)
.HasForeignKey(p => p.TeamId)
.HasPrincipalKey(t => t.Id);
modelBuilder.Entity<Team>()
.HasMany(p => p.Players)
.WithOne(p => p.Team)
.HasForeignKey(p => p.TeamId)
.HasPrincipalKey(t => t.Id);
modelBuilder.Entity<Game>()
.HasOne(g => g.FirstTeam)
.WithMany(t => t.Games)
.HasForeignKey(t => t.FirstTeamId)
.HasPrincipalKey(t => t.Id);
modelBuilder.Entity<Game>()
.HasOne(g => g.SecondTeam)
.WithMany(t => t.Games)
.HasForeignKey(t => t.SecondTeamId)
.HasPrincipalKey(t => t.Id);
}
}
What wrong with this code? Because I have "Navigation properties can only participate in a single relationship." error when I try to do something with my ApplicationContext.
You can't reuse Team.Games as the inverse property for both Game.FirstTeam and Team.SecondTeam. Think of it, if you add game to Team.Games, how would EF know which team it is, first or second?
You need two collections to describe the relationships. And that's also a chance to add some more meaning to the class model. For example (only modified code):
public class Game
{
...
public int HomeTeamId { get; set; }
public Team HomeTeam { get; set; }
public int AwayTeamId { get; set; }
public Team AwayTeam { get; set; }
}
public class Team
{
...
public List<Game> HomeGames { get; set; }
public List<Game> AwayGames { get; set; }
}
For a team it's meaningful to make a distinction between home and away games, for example to compare results in both types of games.
And the mapping:
modelBuilder.Entity<Game>()
.HasOne(g => g.HomeTeam)
.WithMany(t => t.HomeGames)
.HasForeignKey(t => t.HomeTeamId)
.HasPrincipalKey(t => t.Id);
modelBuilder.Entity<Game>()
.HasOne(g => g.AwayTeam)
.WithMany(t => t.AwayGames)
.HasForeignKey(t => t.AwayTeamId).OnDelete(DeleteBehavior.NoAction)
.HasPrincipalKey(t => t.Id);
If using Sql Server, this delete behavior instruction is necessary to prevent disallowed multiple cascade paths.
The problem is that your Team model has 2 one-to-many relationships with your Game model but you only have one navigation property on the Team.
You need to have 2 navigation properties on the Team model, one for each relationship.
(Game1, Game2...).
You will also need to define these relationships in the Game model - a Team property for each relationship.
Check this answer for extra info.

Entity Framework Core: multiple relationships to one table of base type

Let's assume that Administrator, Purchaser and Supplier have User base type and remaining models look following:
public class Vendor
{
public int VendorId { get; set; }
public List<Supplier> Suppliers { get; set; }
}
public class Task
{
public int TaskId { get; set; }
public Administrator Admin { get; set; }
public List<Purchaser> Purchasers { get; set; }
public Vendor Vendor { get; set; }
}
Now I would like to create a UserTask table that contains IDs of all users of the Task: an Admin, Purchasers and Suppliers of the Vendor in column User and their Tasks IDs in column Task.
How could I configure such setup in Fluent API?
Edit:
I created additional entity UserTask that consists of IDs and navigation properties:
public class UserTask
{
public int UserId { get; set; }
public User User { get; set; }
public int TaskId { get; set; }
public Task Task { get; set; }
//some other needed properties
}
And tried to configure models like this:
modelBuilder.Entity<UserTask>(ut =>
{
ut.HasKey(x => new { x.UserId, x.TaskId });
ut.HasOne(u => u.User).WithMany()
.HasForeignKey(u => u.UserId)
.OnDelete(DeleteBehavior.Cascade);
ut.HasOne(t => t.Task).WithMany()
.HasForeignKey(t => t.TaskId)
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity<Task>(t =>
{
t.HasMany(p => p.Purchasers).WithMany(p => p.Tasks);
t.HasOne(a => a.Administrator).WithMany(); //adding a => a.Task expression in parameter throws error that the relationship is already defined
t.HasMany(s => s.Vendors.Suppliers).WithMany(s => s.Tasks); //throws error
});
And it fails because HasMany(s => s.Vendors.Suppliers) i not a valid member access expression. Is there any way to overcome this issue?
Considering the relationships in these tables, add a property so that Fluent API can reference the relationship. About the specific modelbuilder.
modelBuilder.Entity<Supplier>()
.HasOne(x => x.vendor)
.WithMany(y => y.Suppliers);
modelBuilder.Entity<Administrator>()
.HasOne(a => a.tasks)
.WithOne(t => t.Admin)
.HasForeignKey<Administrator>(f=>f.AdministratorId);
modelBuilder.Entity<Vendor>()
.HasOne(a => a.tasks)
.WithOne(t => t.Vendor)
.HasForeignKey<Vendor>(f=>f.VendorId);
The model need to be redesigned as this.
public class User
{
public int id { get; set; }
public string Property { get; set; }
}
public class Vendor
{
public int VendorId { get; set; }
public List<Supplier> Suppliers { get; set; }
public Tasks tasks { get; set; }
}
public class Tasks
{
[Key]
public int TaskId { get; set; }
public Administrator Admin { get; set; }
public List<Purchaser> Purchasers { get; set; }
public Vendor Vendor { get; set; }
}
public class Supplier:User
{
public int SupplierId { get; set; }
public string SupplierProperty { get; set; }
public Vendor vendor { get; set; }
}
public class Administrator:User
{
public int AdministratorId { get; set; }
public string adminProperty { get; set; }
public Tasks tasks { get; set; }
}
public class Purchaser:User
{
public int PurchaserId { get; set; }
public string purProperty { get; set; }
public Tasks tasks { get; set; }
}

How to configure this model relationship using FLUENT API

The user(AskedUser) can have many questions asked by other users(Asker).
Users(Asker) can ask questions to other users(AskedUser).
So the QuestionModel should have foreign key to asked user id and foreign key to user who asked the question.
Do I constructed my models to what I want to achieve? How to configure this using fluent api cause this is not achievable using data annotations only.
public class ApplicationUser : IdentityUser<long>
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string City { get; set; }
public string Country { get; set; }
public DateTime BirthDate { get; set; }
public ICollection<QuestionModel> AskedQuestions { get; set; }
}
public class QuestionModel
{
public int Id { get; set; }
public string Content { get; set; }
public bool IsAnswered { get; set; }
public long AskerId { get; set; }
public ApplicationUser Asker { get; set; }
public long AskedUserId { get; set; }
public ApplicationUser AskedUser { get; set; }
}
This is I've tried so far
builder.Entity<ApplicationUser>()
.HasMany(user => user.AskedQuestions)
.WithOne(q => q.AskedUser)
.HasForeignKey(user => user.AskedUserId)
.HasConstraintName("ForeignKey_User_AskedQuestion")
.HasForeignKey(user => user.AskerId)
.HasConstraintName("ForeignKey_Asker_QuestionAsked")
.IsRequired(true);
You can do it on QuestionModel
//Asker relation
builder.Entity<QuestionModel>()
.HasOne(q=> q.Asker)
.Withmany(u => u.AskedQuestions)
.HasForeignKey(q=> q.AskerId)
.HasConstraintName("ForeignKey_Asker_QuestionAsked")
.IsRequired(true);
//Asked relation
builder.Entity<QuestionModel>()
.HasOne(q=> q.AskedUser)
.Withmany()
.HasForeignKey(q=> q.AskeduserId)
.HasConstraintName("ForeignKey_User_AskedQuestion")
.IsRequired(true);
I use Fluent API on dependant model instead of the root element.

Entity Framework Core Navigation Properties Error

I'm trying to make a simple app to try Entity Framework Core, but i a have problem with setting up relations between entities. My entities:
public class Card
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Adress { get; set; }
public DateTime DoB { get; set; }
public DateTime DoS { get; set; }
public User Portal { get; set; }
public List<Reservation> Res { get; set; }
}
public class Doctor
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public TimeSpan Start_Working { get; set; }
public TimeSpan End_Working { get; set; }
public List<Reservation> Reservations { get; set; }
public int SpecID { get; set; }
public Spec Spec { get; set; }
}
public class Reservation
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime DoR { get; set; }
public string Info { get; set; }
public int CardID { get; set; }
public Card Card_Nav_R { get; set; }
public int DoctorID { get; set; }
public Doctor Doctor { get; set; }
}
public class Spec
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public List<Doctor> Doctors { get; set; }
}
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public int CardID { get; set; }
public Card Card { get; set; }
}
And a configuration class where i tried to set up relations:
class ApplicationContext:DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Card> Cards { get; set; }
public DbSet<Reservation> Reservations { get; set; }
public DbSet<Doctor> Doctors { get; set; }
public DbSet<Spec> Specs { get; set; }
public ApplicationContext()
{
Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder ModelBuilder)
{
ModelBuilder.Entity<User>().HasKey(u => u.Id);
ModelBuilder.Entity<Card>().HasKey(c => c.Id);
ModelBuilder.Entity<Doctor>().HasKey(d => d.Id);
ModelBuilder.Entity<Spec>().HasKey(s => s.Id);
ModelBuilder.Entity<Reservation>().HasKey(r => r.Id);
ModelBuilder.Entity<User>().Property(u => u.Email).IsRequired();
ModelBuilder.Entity<User>().Property(u => u.Password).IsRequired();
ModelBuilder.Entity<Card>().Property(c => c.Name).IsRequired();
ModelBuilder.Entity<Card>().Property(c => c.Surname).IsRequired();
ModelBuilder.Entity<Card>().Property(c => c.DoB).IsRequired();
ModelBuilder.Entity<Card>().Property(c => c.Adress).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Name).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Surname).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Spec).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Email).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.Start_Working).IsRequired();
ModelBuilder.Entity<Doctor>().Property(d => d.End_Working).IsRequired();
ModelBuilder.Entity<Reservation>().Property(r => r.Info).IsRequired();
ModelBuilder.Entity<Reservation>().Property(r => r.Card_Nav_R).IsRequired();
ModelBuilder.Entity<Reservation>().Property(r => r.Doctor).IsRequired();
ModelBuilder.Entity<Reservation>().Property(r => r.DoR).IsRequired();
ModelBuilder.Entity<Spec>().Property(s => s.Name).IsRequired();
ModelBuilder.Entity<Doctor>().HasOne<Spec>(d=>d.Spec).WithMany(s => s.Doctors).HasForeignKey(d => d.SpecID);
ModelBuilder.Entity<User>().HasOne<Card>(u => u.Card).WithOne(c => c.Portal).HasForeignKey<User>(u => u.CardID);
ModelBuilder.Entity<Reservation>().HasOne<Card>(r => r.Card_Nav_R).WithMany(c => c.Res).HasForeignKey(r => r.CardID);
ModelBuilder.Entity<Reservation>().HasOne<Doctor>(r => r.Doctor).WithMany(d => d.Reservations).HasForeignKey(r => r.DoctorID);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Simple_Try;Trusted_Connection=True;");
}
}
So, when i tried to add migration or add something to database i saw this error:
System.InvalidOperationException: 'The property or navigation 'Spec' cannot be added to the entity type 'Doctor' because a property or navigation with the same name already exists on entity type 'Doctor'.'
I really don't know how to fix this, i tried to use annotations instead of Fluent API, but had the same result.
The cause of the exception is the following line:
ModelBuilder.Entity<Doctor>().Property(d => d.Spec).IsRequired();
because Doctor.Spec is a navigation property
public class Doctor
{
// ...
public Spec Spec { get; set; }
}
and navigation properties cannot be configured via Property fluent API.
So simply remove that line. Whether reference navigation property is required or optional is controlled via relationship configuration. In this case
ModelBuilder.Entity<Doctor>()
.HasOne(d => d.Spec)
.WithMany(s => s.Doctors)
.HasForeignKey(d => d.SpecID)
.IsRequired(); // <--
although the IsRequired is automatically derived from the FK property type - since SpecID is non nullable, then the relationship is required.
For more info, see Required and Optional Properties and Required and Optional Relationships documentation topics.

Entity Framework One to One Fluent Api Can not Delete Data

That is the Gift class in model. That should be the parent class.
public class Gift
{
public int GiftId { get; set; }
public string Title { get; set; }
public string Brand { get; set; }
public double Price { get; set; }
public bool Chosen { get; set; }
public virtual Shop Shop { get; set; }
public virtual Person Person { get; set; }
}
That is the Shop class and these two have one to one relationship. A gift shoud have a shop, and a shop should have a gift.
public class Shop
{
public int ShopId { get; set; }
public string Name { get; set; }
public string Street { get; set; }
public string Number { get; set; }
public string Postcode { get; set; }
public string District { get; set; }
public virtual Gift Gift { get; set; }
}
That is the third class in my model. This class has one to zero relationship with the gift class. If a gift is not chosen, it does not have any person. Same for the person too.
public class Person
{
public int Id { get; set; }
public string FirstName{ get; set; }
public string Surname{ get; set; }
public string EmailAdress { get; set; }
public virtual Gift Gift { get; set; }
}
Here is the fluent api that i have lots of times changed.
modelBuilder.Entity<Gift>()
.HasOptional(x => x.Person);
modelBuilder.Entity<Person>()
.HasRequired(x => x.Gift);
modelBuilder.Entity<Gift>()
.HasRequired(x => x.Shop).WithOptional(x => x.Gift).Map(x => x.MapKey("ShopId"));
modelBuilder.Entity<Shop>()
.HasRequired(x => x.Gift).WithOptional(x => x.Shop).Map(x => x.MapKey("GiftId"));
I can save data but when i want to delete a gift, i can not succeed and have problems. How can i fix that? Thanx already!
I have fixed it. Here is the link
modelBuilder.Entity<Shop>()
.HasRequired(x => x.Gift)
.WithRequiredDependent();
modelBuilder.Entity<Gift>()
.HasRequired(x => x.Shop)
.WithRequiredPrincipal();
base.OnModelCreating(modelBuilder);

Categories