Problem with association when using EntityFramework - entity-framework-4

I'm currently testing EntityFramework 4.1.
My problem is, I have a Team class and a Game class.
class Team {
string Name { get; set; }
}
class Game {
Team HomeTeam { get; set; }
Team AwayTeam { get; set; }
}
Still haven't found a way to map this. I've managed to do so using NHibernate but no luck with EF. Any pointers?

This is maps cleanly for me:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Team>().HasKey(t => t.Name);
modelBuilder.Entity<Game>().HasKey(
g => new { g.AwayTeamName, g.HomeTeamName });
modelBuilder.Entity<Game>()
.HasRequired(g => g.HomeTeam)
.WithMany()
.HasForeignKey(g => g.HomeTeamName)
.WillCascadeOnDelete(true);
modelBuilder.Entity<Game>()
.HasRequired(g => g.AwayTeam)
.WithMany()
.HasForeignKey(g => g.AwayTeamName)
.WillCascadeOnDelete(false);
base.OnModelCreating(modelBuilder);
}
Note that you cannot cascade both PK/FK relationships. If you were successful with NHib, you will know that cascading both would cause cycles.
EDIT: I guess I should include my POCOs:
public class Team
{
public string Name { get; set; }
}
public class Game
{
internal string HomeTeamName { get; set; }
public Team HomeTeam { get; set; }
internal string AwayTeamName { get; set; }
public Team AwayTeam { get; set; }
}
Note that you MUST have some sort of key on your POCOs, hence the need for the HomeTeamName and AwayTeamName. You can keep them internal if you want to hide them from the consuming code.

Related

EF Core 2.2 - Two foreign keys to same table

I have a similar problem to the one posted here:
Entity Framework Code First - two Foreign Keys from same table, however it's very old and doesn't apply to Core and I can't get the suggestions to work for me.
Basically, I'm trying to create a fixture table which will have two foreign keys to the team table. A fixture is made up of a home team and an away team. Having nullable fields isn't an option.
Consider a fixture, with two teams.
public class Fixture
{
public int Id { get; set; }
public Team HomeTeam { get; set; }
public int HomeTeamId { get; set; }
public Team AwayTeam { get; set; }
public int AwayTeamId { get; set; }
public virtual Team HomeTeam { get; set; }
public virtual Team AwayTeam { get; set; }
}
public class Team
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public String Name { get; set; }
public ICollection<Fixture> HomeFixtures { get; set; } = new List<Fixture>();
public ICollection<Fixture> AwayFixtures { get; set; } = new List<Fixture>();
}
I get the error...
Unable to determine the relationship represented by navigation property 'Fixture.HomeTeam' of type 'Team'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
So I tried to add some OnModelCreating code in the database context:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Fixture>()
.HasOne(m => m.HomeTeam)
.WithMany(t => t.HomeFixtures)
.HasForeignKey(m => m.HomeTeamId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Fixture>()
.HasOne(m => m.AwayTeam)
.WithMany(t => t.AwayFixtures)
.HasForeignKey(m => m.AwayTeamId)
.OnDelete(DeleteBehavior.Restrict);
}
Then I got the error:
Introducing FOREIGN KEY constraint 'FK_Fixtures_Teams_HomeTeamId' on table 'Fixtures' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Can anyone help with getting this setup please?
Thanks.
public class Fixture
{
public int Id { get; set; }
public int HomeTeamId { get; set; }
public int AwayTeamId { get; set; }
[ForeignKey("HomeTeamId")]
public virtual Team HomeTeam { get; set; }
[ForeignKey("AwayTeamId")]
public virtual Team AwayTeam { get; set; }
}
This way navigation will work. Also as suggested by #Ivan remove duplicate getters and setters.
Solution below worked for me for EF Core 3:
Make sure to make foreign keys nullable
Specify default behavior on Delete
public class Match
{
public int? HomeTeamId { get; set; }
public int? GuestTeamId { get; set; }
public float HomePoints { get; set; }
public float GuestPoints { get; set; }
public DateTime Date { get; set; }
public Team HomeTeam { get; set; }
public Team GuestTeam { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Match>()
.HasRequired(m => m.HomeTeam)
.WithMany(t => t.HomeMatches)
.HasForeignKey(m => m.HomeTeamId)
.OnDelete(DeleteBehavior.ClientSetNull);
modelBuilder.Entity<Match>()
.HasRequired(m => m.GuestTeam)
.WithMany(t => t.AwayMatches)
.HasForeignKey(m => m.GuestTeamId)
.OnDelete(DeleteBehavior.ClientSetNull);
}

Defining multiple Foreign Key for the Same table in Entity Framework Code First

I have two entities in my MVC application and I populated the database with Entity Framework 6 Code First approach. There are two city id in the Student entity; one of them for BirthCity, the other for WorkingCity. When I define the foreign keys as above an extra column is created named City_ID in the Student table after migration. Id there a mistake or how to define these FKs? Thanks in advance.
Student:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int BirthCityID { get; set; }
public int LivingCityID { get; set; }
[ForeignKey("BirthCityID")]
public virtual City BirthCity { get; set; }
[ForeignKey("LivingCityID")]
public virtual City LivingCity { get; set; }
}
City:
public class City
{
public int ID { get; set; }
public string CityName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
To achieve what you want you need to provide some aditional configuration.Code First convention can identify bidirectional relationships, but not when there are
multiple bidirectional relationships between two entities.You can add configuration (using Data Annotations or the Fluent API) to present this
information to the model builder. With Data Annotations, you’ll use an annotation
called InverseProperty. With the Fluent API, you’ll use a combination of the Has/With methods to specify the correct ends of these relationships.
Using Data Annotations could be like this:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int BirthCityID { get; set; }
public int LivingCityID { get; set; }
[ForeignKey("BirthCityID")]
[InverseProperty("Students")]
public virtual City BirthCity { get; set; }
[ForeignKey("LivingCityID")]
public virtual City LivingCity { get; set; }
}
This way you specifying explicitly that you want to relate the BirthCity navigation property with Students navigation property in the other end of the relationship.
Using Fluent Api could be like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().HasRequired(m => m.BirthCity)
.WithMany(m => m.Students).HasForeignKey(m=>m.BirthCityId);
modelBuilder.Entity<Student>().HasRequired(m => m.LivingCity)
.WithMany().HasForeignKey(m=>m.LivingCityId);
}
With this last solution you don't need to use any attibute.
Now, the suggestion of #ChristPratt in have a collection of Student in your City class for each relationship is really useful. If you do that, then the configurations using Data Annotations could be this way:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int BirthCityID { get; set; }
public int LivingCityID { get; set; }
[ForeignKey("BirthCityID")]
[InverseProperty("BirthCityStudents")]
public virtual City BirthCity { get; set; }
[ForeignKey("LivingCityID")]
[InverseProperty("LivingCityStudents")]
public virtual City LivingCity { get; set; }
}
Or using Fluent Api following the same idea:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().HasRequired(m => m.BirthCity)
.WithMany(m => m.BirthCityStudents).HasForeignKey(m=>m.BirthCityId);
modelBuilder.Entity<Student>().HasRequired(m => m.LivingCity)
.WithMany(m => m.LivingCityStudents).HasForeignKey(m=>m.LivingCityId);
}
Sheesh. It's been a long day. There's actually a very big, glaring problem with your code, actually, that I completely missed when I commented.
The problem is that you're using a single collection of students on City. What's actually happening here is that EF can't decide which foreign key it should actually map that collection to, so it creates another foreign key specifically to track that relationship. Then, in effect you have no navigation properties for the collections of students derived from BirthCity and LivingCity.
For this, you have to drop down to fluent configuration, as there's no way to configure this properly using just data annotations. You'll also need an additional collection of students so you can track both relationships:
public class City
{
...
public virtual ICollection<Student> BirthCityStudents { get; set; }
public virtual ICollection<Student> LivingCityStudents { get; set; }
}
Then, for Student:
public class Student
{
...
public class StudentMapping : EntityTypeConfiguration<Student>
{
public StudentMapping()
{
HasRequired(m => m.BirthCity).WithMany(m => m.BirthCityStudents);
HasRequired(m => m.LivingCity).WithMany(m => m.LivingCityStudents);
}
}
}
And finally in your context:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new Student.StudentMapping());
}

Navigation property no more working after migration of breeze 1.4.2

I work with asp.net mvc with Durandal & breeze templates.
I have the following code-first classes:
1st scenario:
public class Transport
{
[Key]
public int Id { get; set; }
...
public int? SenderId { get; set; }
public virtual SendRecv Sender { get; set; }
}
public class SendRecv
{
[Key]
public int Id { get; set; }
...
public virtual List<Transport> Transports { get; set; }
}
Then I can easily get all my related transports from my observable of type SendRecv:
if (sendRecvs()[i].transports().length > 0)
{
...
}
The problem: I add a new reference to the same entity SendRecv in my Transport entity like this:
2nd scenario:
public class Transport
{
[Key]
public int Id { get; set; }
...
public int? SenderId { get; set; }
public int? ReceiverId { get; set; }
public virtual SendRecv Sender { get; set; }
public virtual SendRecv Receiver { get; set; }
}
Then I cannot get my related transports anymore! The navigation property named transport from my SendRecv entity does not exists anymore.
This don't work anymore:
if (sendRecvs()[i].transports().length > 0)
{
...
}
PS: I'm pretty sure this worked before the migration of breeze to 1.4.2
Any idea?
Thanks.
I'm assuming you only want one collection of Transport in SendRecv.
If so, try this (using fluent API):
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<SendRecv>()
.HasMany(c => c.Transports)
.WithRequired(c => c.Sender)
.WillCascadeOnDelete(false);
modelBuilder.Entity<SendRecv>()
.HasMany(c => c.Transports)
.WithRequired(c => c.Receiver)
.WillCascadeOnDelete(false);
}
Now, if you want 2 Transport lists (i.e. Transports1 and Transports2) then try:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<SendRecv>()
.HasMany(c => c.Transports1)
.WithRequired(c => c.Sender)
.WillCascadeOnDelete(false);
modelBuilder.Entity<SendRecv>()
.HasMany(c => c.Transports2)
.WithRequired(c => c.Receiver)
.WillCascadeOnDelete(false);
}

Entity Framework Data Annotations equivalent of .WillCascadeOnDelete(false);

I'm currently using EF Code First 4.3 with migrations enabled, but automatic migrations disabled.
My question is simple, is there a data annotations equivalent of the model configuration .WillCascadeOnDelete(false)
I would like to decorate my class so that the foreign key relationships do NOT trigger a cascading delete.
Code sample:
public class Container
{
public int ContainerID { get; set; }
public string Name { get; set; }
public virtual ICollection<Output> Outputs { get; set; }
}
public class Output
{
public int ContainerID { get; set; }
public virtual Container Container { get; set; }
public int OutputTypeID { get; set; }
public virtual OutputType OutputType { get; set; }
public int Quantity { get; set; }
}
public class OutputType
{
public int OutputTypeID { get; set; }
public string Name { get; set; }
}
I Would like to do something like this:
public class Output
{
[CascadeOnDelete(false)]
public int ContainerID { get; set; }
public virtual Container Container { get; set; }
[CascadeOnDelete(false)]
public int OutputTypeID { get; set; }
public virtual OutputType OutputType { get; set; }
public int Quantity { get; set; }
}
This way i would be able to scaffold the migration correctly. which scaffolds the foreign key relationships to be cascade deleted at the moment.
Any ideas, other than using Model Configuration?
No there is no such equivalent. You must use fluent API to remove cascade delete selectively or you must remove OneToManyCascadeDelete convention to remove it globally.
Create a mapping class (the fluent syntax) and use the code below:
// add relationships "Post" and "User" to a "Comment" entity
this.HasRequired(t => t.Post)
.WithMany(t => t.Comments)
.HasForeignKey(d => d.PostID)
.WillCascadeOnDelete(false); // <---
this.HasOptional(t => t.User)
.WithMany(t => t.Comments)
.HasForeignKey(d => d.UserID)
.WillCascadeOnDelete(false); // <---
Here's a nice post on how to set up fluent mappings if you need more info.
Just make the FK property nullable can prevent cascade delete from happening:
public int? OutputTypeID { get; set; }

Data Model help for an administrator class

I am trying to build a data model that I can use with Entity Framework 4.1.
I am tring to build a simple app to manage events (like a birthday party). So I figure I will have two types of users, Admins and Attenders. The admins will be able to create and manage the event and the attenders will only be able to view an event they are invited to.
I thought I only needed 2 classes for this but I am not sure. Here is waht I did for EF4.1
public class user
{
public int id { get; set; }
public string name { get; set; }
public ICollection<myevent> myadminevents { get; set; }
public ICollection<myevent> myinvites { get; set; }
}
public class myevent {
public int id { get; set; }
public string name { get; set; }
public ICollection<user> admin { get; set; }
public ICollection<user> attend { get; set; }
}
public class myeventcontext : DbContext
{
public DbSet<user> users { get; set; }
public DbSet<myevent> events { get; set; }
}
EF didnt do what I thought it would. It is ignoring my collections. So I don't think the model is right.
Any suggestions?
You can model this using two junction tables and configure the many-to-many relationships using fluent API
public class myeventcontext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Entity<User>()
.HasMany(user => user.myinvites).WithMany(event => event.attend)
.Map(m =>
{
m.ToTable("EventAttendees");
m.MapLeftKey("UserId");
m.MapRightKey("EventId");
});
Entity<User>()
.HasMany(user => user.myadminevents).WithMany(event => event.admin)
.Map(m =>
{
m.ToTable("EventAdmins");
m.MapLeftKey("UserId");
m.MapRightKey("EventId");
});
}
}
Other approach would be to have a single junction table with an additional column to store whether attendee is is an admin or not. But in this way you will not be able model many-to-many relationship without including the junction table as a class.

Resources