EF Core 2.1 'UNIQUE constraint failed: People.ReferredById' - entity-framework

My Data Model
Table("People")]
public abstract class Person
{
public long Id { get; set; }
public string Name { get; set; }
[ForeignKey("ReferredBy")]
public long? ReferredById { get; set; }
public User ReferredBy { get; set; }
}
public class User : Person
{
public string UserName { get; set; }
}
public class Student : Person
{
public string RollNo { get; set; }
}
Fluent API to set null on delete
modelBuilder.Entity<Person>()
.HasOne(t => t.ReferredBy)
.WithOne()
.OnDelete(DeleteBehavior.SetNull);
Now when i try to insert a row with same ReferredByit throws an unique constraint error. Please help me with this.

Related

Assp.net Core exeption ('Invalid column name 'ClassRoomClassId')

I have a problem with ASP.NET Core scaffolding. I just applied scaffolding on a simple relational model and I got this SqlException.
Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'ClassRoomClassId'.'
All I have two models
ClassRoom
public class ClassRoom
{
[Key]
public int ClassId { get; set; }
public string ClassName { get; set; }
public virtual ICollection<Student> Student { get; set; }
}
Student
public class Student
{
[Key]
public int StuId { get; set; }
public string StuName { get; set; }
[ForeignKey("ClassId")]
public int? ClassId { get; set; }
public ClassRoom ClassRoom { get; set; }
}
And I get the exception when executing Students/Index.
// GET: Students
public IActionResult Index()
{
return View(_context.Student.ToList());
}
I have no column with this name 'ClassRoomClassId' !!
what would be the problem?
Try changing [ForeignKey("ClassId")] to [ForeignKey("ClassRoom")] or placing it on ClassRoom property:
public class Student
{
...
public int? ClassId { get; set; }
[ForeignKey("ClassId")]
public ClassRoom ClassRoom { get; set; }
}

Entity Framework attempting to select non-existing column

I'm using Entity Framework 6.0 and have defined 2 POCO's to map to my database:
[Table("PortfolioGroups")]
public class PortfolioGroup : AuditableEntity<int>
{
[Column("Company_Id")]
public int CompanyId { get; set; }
[ForeignKey("CompanyId")]
public Company Company { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection<PortfolioGroupItem> PortfolioGroupItems { get; set; }
public PortfolioGroup()
{
PortfolioGroupItems = new Collection<PortfolioGroupItem>();
}
}
And the PortfolioGroupItem:
[Table("PortfolioGroupItems")]
public class PortfolioGroupItem : AuditableEntity<int>
{
[Column("PortfolioGroup_Id")]
public int PortfolioGroupId { get; set; }
[ForeignKey("PortfolioGroupId")]
public PortfolioGroup PortfolioGroup { get; set; }
[Column("Trademark_Id")]
public int? TrademarkId { get; set; }
[ForeignKey("TrademarkId")]
public Trademark.Trademark Trademark { get; set; }
[Column("TrademarkRegistration_Id")]
public int? TrademarkRegistrationId { get; set; }
[ForeignKey("TrademarkRegistrationId")]
public TrademarkRegistration TrademarkRegistration { get; set; }
[Column("Domain_Id")]
public int? DomainId { get; set; }
[ForeignKey("DomainId")]
public Domains.Domain Domain { get; set; }
}
However - when I attempt to query the PortfolioGroups, Entity Framework for some reason attempts to query a field named "Trademark_Id" - which doesn't exist on the PortfolioGroup entity:
Context.PortfolioGroups.SingleOrDefault(i => i.Id == id && i.CompanyId == companyId);
Throws:
Invalid column name 'Trademark_Id'.
I've used this kind of setup other places in my application without any problems. I simply cannot find out why EF is trying to query a column that's not in my entity!
Any suggestions would be greatly appreciated. I'm at the end of my rope here.
Thanks guys! :)
The problem is that you've added a Navigation Property on Trademark that requires a Foreign Key on Portfolio Group:
public class Trademark
{
[Key]
public int Id { get; set; }
[MaxLength(250)]
[Required]
public string Name { get; set; }
[MaxLength(150)]
public string Filename { get; set; }
public ICollection<PortfolioGroup> PortfolioGroups { get; set; }
public Trademark()
{
PortfolioGroups = new Collection<PortfolioGroup>();
}
}
EF expects PortfolioGorup to have a Trademark_ID column to store which PortfolioGroups are associated with a Trademark.

Entity Framework Code first creates unexpected Tables and Relationships

Using EntityFramework 6.1.3, I've got the following
public class RacesContext:DbContext
{
public DbSet<Race> Races { get; set; }
public DbSet<Sailboat> Sailboats { get; set; }
public DbSet<VenueParticipation> VenueParticipations { get; set; }
}
public class Crew
{
public int CrewId { get; set; }
public string Name { get; set; }
}
public class Sailboat
{
[Key]
public int SailboatId { get; set; }
public string Name { get; set; }
public string Skipper { get; set; }
public virtual ICollection<Crew> BoatCrew { get; set; }
}
public class VenueParticipation
{
[Key]
public int Id { get; set; }
public virtual ICollection<Sailboat> Boats { get; set; }
public virtual ICollection<Race> Races { get; set; }
}
public class Race
{
[Key]
public int RaceId { get; set; }
public string Venue { get; set; }
public DateTime Occurs { get; set; }
}
EF creates the Creates the Crews table with the proper PK and FK as I would expect. But creates the Races Sailboats, VenueParticipations tables in an unexpected way. Sailboats get's the expected PK but the unexpected FK VenueParticipation_Id as does Races. I was expecting the VenueParticipations table to get FKs to the others allowing a many to many relationship.. I'm sure I'm missing something here. Any advice would be great.
You can either configure the joining tables VenueParticipationSailboat, VenueParticipationRace with the proper FKs or you can use the fluent API:
modelBuilder.Entity<VenueParticipation>()
.HasMany(t => t.Sailboats)
.WithMany(t => t.VenueParticipations)
.Map(m =>
{
m.ToTable("VenueParticipationSailboat");
m.MapLeftKey("VenueParticipationID");
m.MapRightKey("SailboatID");
});
https://msdn.microsoft.com/en-us/data/jj591620.aspx#ManyToMany

Entity Framework table and not Entity Framework mapping

In database present two table
OLD, not EF, modification denied
Object (Id, Name)
UserObjects (Id, UserId, ObjectId)
public class UserObject
{
[Key]
public int Id { get; set; }
public Guid UserId { get; set; }
public int ObjectId { get; set; }
[NotMapped]
public string ObjectName { get; set; }
}
There are way to fill ObjectName with EF?
Or use SqlCommand and fill UserObject manually?
I found and use EntityTypeConfiguration
public class Object
{
public int Id { get; set; }
public string Name { get; set; }
}
public class UserObject
{
[Key]
public int Id { get; set; }
public Guid UserId { get; set; }
public int ObjectId { get; set; }
public virtual Object Object { get; set; }
}
public class ObjectConfiguration: EntityTypeConfiguration<Object>
{
public ObjectConfiguration()
{
HasKey(at => new { at.Id});
Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
ToTable("Objects");
Property(t => t.Id).HasColumnName("Id");
Property(t => t.Name).HasColumnName("Name");
}
}

EF 4.0 - CodeFirst One To Many - Fluent API

I have the following two classes:
public class Person
{
public int Id { get; set; }
public string FullName { get; set; }
}
public class Trip
{
public int Id { get; set; }
public string Name { get; set; }
public virtual IEnumerable<Person> Persons { get; set; }
}
As you can see, a Trip can have 1 or more Persons...
I tried to use the EntityConfiguration to build the database properly but I cannot manage to make it work... I am quite confused on its usage:
public class TripConfiguration : EntityTypeConfiguration<Trip>
{
internal TripConfiguration()
{
// ???
}
}
What do I need to write to have the application to behave properly:
I need at least one person.
I might have more that one person
A person cannot be in the SAME trip twice
A person can be in more than one trip
Try this:
this.HasRequired(x => x.Person)
.WithMany(x => x.Trips)
.HasForeignKey(x => x.PersonId);
Your classes:
public class Person
{
public int Id { get; set; }
public string FullName { get; set; }
public virtual ICollection<Trip> Trips { get; set;}
}
public class Trip
{
public int Id { get; set; }
public string Name { get; set; }
public int PersonId { get; set; }
public virtual Person Person { get; set; }
}
And as far that I know, EF doesn't support unique FK (or correct me if I'm wrong..). So you have to check it yourself.
This is not a One-To-Many relationship, this is a Many-To-Many relationship, you need to have collections on both sides of the relationship. EF will create the joiner table on your behalf. Since today you cannot configure a person being in a trip only once you will need to create a unique constraint in your joiner table once is created to assure this happens since EF does not yet support Unique Key constraints through configuration.
public class Person
{
public int Id { get; set; }
public string FullName { get; set; }
public virtual ICollection<Trip> Trips { get; set; }
}
public class Trip
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
then
class PersonConfiguration : EntityTypeConfiguration<Person>
{
public PersonConfiguration()
{
this.HasMany(t => t.Trips).WithMany(t => t.Persons);
}
}