FK column to mulitple tables POCO - c#

I'm trying to stay in POCO, but I'm at an impasse:
Public class Alpha()
{
public int ID { get; set; }
[ForeignKey("")]
public int BetaId { get; set; }
public virtual BetaA BetaA{ get; set; }
public virtual BetaB BetaB{ get; set; }
}
Public class BetaA()
{
public int ID { get; set; }
}
Public class BetaB()
{
public int ID { get; set; }
}
How do I get Alpha.BetaId to be both ForeignKey to BetaA.ID and BetaB.ID in POCO?
If anyone else stumble around this
Solution I used
Public class Alpha()
{
public int ID { get; set; }
public int BetaId { get; set; }
[ForeignKey("BetaId")]
public virtual BetaA BetaA{ get; set; }
[ForeignKey("BetaId")]
public virtual BetaB BetaB{ get; set; }
}
Public class BetaA()
{
public int ID { get; set; }
}
Public class BetaB()
{
public int ID { get; set; }
}

have BetaA and BetaB share a primary key then reference BetaB via BetaA
You can also decorate the nav properties with the foreign key attribute and specify the int property as the FK for both, though I have not tested this scenario myself (I'm on a mobile device). Give it a shot and let me know!

Related

I got a the entity type requires primary key error

When i try to connect with my database and my class i got this error
But this error appear just for my Consoles,KeyboardMouse and Headphones tables. But they have already primary keys.
and here is my context class
public class EcommContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(#"Server=.;Database=eCommerce;Trusted_Connection=true");
}
public DbSet<Product> Products { get; set; }
public DbSet<Card> Cards { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Consoles> Consoles { get; set; }
public DbSet<Headphone> Headphones { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderDetail> OrderDetails { get; set; }
public DbSet<Mouse> Mouses { get; set; }
public DbSet<MousePad> MousePads { get; set; }
public DbSet<Keyboard> Keyboards { get; set; }
public DbSet<KeyboardAndMouse> KeyboardMouse { get; set; }
public DbSet<Gamepad> Gamepads { get; set; }
public DbSet<Computer> Computers { get; set; }
}
And my entity classes
public class Headphone:IEntity
{
public int HeadphonesId { get; set; }
public int ProductId { get; set; }
public bool IsWireless { get; set; }
public string Type { get; set; }
public string Color { get; set; }
public bool IsGaming { get; set; }
}
public class KeyboardAndMouse:IEntity
{
public int KmId { get; set; }
public int ProductId { get; set; }
public string Color { get; set; }
}
public class Consoles:IEntity
{
public int ConsoleId { get; set; }
public int ProductId { get; set; }
public string Color { get; set; }
public int GamepadNumber { get; set; }
public int Capacity { get; set; }
}
How can I solve that. Does anyone help me ?
In your entity class you need to use [Key] annotation for primary key field. Try like below.
public class Headphone:IEntity
{
[Key]
public int HeadphonesId { get; set; }
}
public class KeyboardAndMouse:IEntity
{
[Key]
public int KmId { get; set; }
}
public class Consoles:IEntity
{
[Key]
public int ConsoleId { get; set; }
}
Please check this out for more information : https://learn.microsoft.com/en-us/ef/core/modeling/keys?tabs=data-annotations#configuring-a-primary-key
FYI - By convention, a property named Id or <type name>Id will be configured as the primary key of an entity.
So if you had HeadphoneId field in Headphone class then it will select that column as primary key by convention and no need to use [Key] annotation or Fluent API to define Primary key field.

Custom Relationship in Entity Framework

I have got the following three tables (just an example)
public class User
{
public int id { get; set; }
public string name { get; set; }
public List<Code> Codes { get; set; }
}
public class Device
{
public int id { get; set; }
public string name { get; set; }
public List<Code> Codes { get; set; }
}
public class Code
{
public int id { get; set; }
public int entity_id { get; set; }
public string entity_type { get; set; }
public string code { get; set; }
}
Now a code can either belong to a User or a Device, which will be determined by the value of entity_type (i.e. 'user' or 'device'). How can this be achieved in Entity Framework ?
You could change your Code class to
public class Code
{
public int id { get; set; }
public User user { get; set; }
public Device device { get; set; }
public string code { get; set; }
}
This way one of those properties can be null.
Hope it helps.
The drawback is that you could have a code that have a user an a device

Entity Framework Code First - Mapping a table to another with both single and multiple relations?

I am trying to set up a relationship where a Car can have multiple Testruns, as well as an optionally selected "active testrun".
public class Car
{
public int ID { get; set; }
public virtual TestRun ActiveTestRun { get; set; }
public virtual ICollection<TestRun> TestRuns { get; set; }
}
public class TestRun
{
public int ID { get; set; }
public double TopSpeed { get; set; }
public virtual Car ActiveCar { get; set; }
public virtual Car Car { get; set; }
}
I have been trying to use InverseProperties with or without ForeignKey-attributes but to no luck. What is the correct way to setup this kind of relation? TIA!
Ps. I think I tried mostly all of the combinations from this tutorial without getting it to work :(
http://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in-code-first.aspx
EDIT: The resulting DB-schema I would want is something like:
tbl_Cars:
ID, ActiveTestRunID[Nullable]
tbl_TestRuns:
ID, TopSpeed, CarID
This achieved the result that I wanted:
public class Car
{
public int ID { get; set; }
public int? ActiveTestRunID { get; set; }
public virtual TestRun ActiveTestRun { get; set; }
[InverseProperty("Car")]
public virtual ICollection<TestRun> TestRuns { get; set; }
}
public class TestRun
{
public int ID { get; set; }
public double TopSpeed { get; set; }
public int CarID { get; set; }
public virtual Car Car { get; set; }
}

How do I establish a one-to-one relationship using Database First?

When my models were generated, many relationships were mapped automagically. However, some of the relationships are "incorrect" (or at least, not what I want), or missing.
I don't doubt that this is because of poor database design, but based on my role in this project there is not much I can do to fix that. However, is there something I can do in my application code to fix the mapping?
Here is one example:
I would like to map the StoreProductId property to the StoreProducts table.
ProductAttributePriceAdjustment
public partial class ProductAttributePriceAdjustment
{
public int AdjustmentId { get; set; }
public int StoreProductId { get; set; }
public int StoreId { get; set; }
public string ProductId { get; set; }
public Nullable<int> ProductSizeId { get; set; }
public Nullable<decimal> Adjustment { get; set; }
public int PointsAdjustment { get; set; }
public Nullable<int> ProductColorID { get; set; }
public StoreProduct StoreProduct { get; set; }
}
StoreProduct
public partial class StoreProduct
{
public int StoreProductID { get; set; }
public int StoreID { get; set; }
public string ProductID { get; set; }
public bool Featured { get; set; }
public bool Clearance { get; set; }
}
In my view, when I try calling something like:
#adjustment.StoreProduct.ProductID
I get this error:
Object reference not set to an instance of an object.
Update 1
I followed Frans' advice and updated my model to this:
public partial class ProductAttributePriceAdjustment
{
public int AdjustmentId { get; set; }
public int StoreProductId { get; set; }
public int StoreId { get; set; }
public string ProductId { get; set; }
public Nullable<int> ProductSizeId { get; set; }
public Nullable<decimal> Adjustment { get; set; }
public int PointsAdjustment { get; set; }
public Nullable<int> ProductColorID { get; set; }
public virtual StoreProduct StoreProduct { get; set; }
}
but am still getting the same error.
You cannot create a 1:1 mapping in entity framework like this. It's not supported.
Entity Framework only supports 1:1 mappings in which both tables have a shared primary key (ie they have the same primary key, and one of them is a foreign key to the other). In your situation, you are actually creating a 1 to many, because there is no guarantee that StoreProductId is unique.

EF - Code First FOREIGN KEY constraint may cause cycles or multiple cascade paths

I have been up to trying to do some Entity Framework Code First, but I am stuck with the 'FOREIGN KEY constraint may cause cycles or multiple cascade paths.' problem.
Here are my classes:
public class Course
{
public Course()
{
this.Subject = new HashSet<Subject>();
this.Student = new HashSet<Student>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Shift { get; set; }
public int Room { get; set; }
public virtual ICollection<Subject> Subject { get; set; }
public virtual ICollection<Student> Student { get; set; }
}
public class Subject
{
public Subject()
{
this.Deliverable = new HashSet<Deliverable>();
}
public int Id { get; set; }
public string Name { get; set; }
public int StartsAt { get; set; }
public int FinishesAt { get; set; }
public System.TimeSpan Weekdays { get; set; }
public int CourseId { get; set; }
public int TeacherId { get; set; }
public virtual Course Course { get; set; }
public virtual Teacher Teacher { get; set; }
public virtual ICollection<Deliverable> Deliverable { get; set; }
}
public class Student : Person
{
public Student()
{
this.Deliverable = new HashSet<Deliverable>();
}
public decimal Average { get; set; }
public int CourseId { get; set; }
public virtual Course Course { get; set; }
public virtual ICollection<Deliverable> Deliverable { get; set; }
}
public class Teacher : Person
{
public Teacher()
{
this.Subject = new HashSet<Subject>();
}
public virtual ICollection<Subject> Subject { get; set; }
}
public class Deliverable
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Mark { get; set; }
public System.DateTime DeliveredDate { get; set; }
public bool Delivered { get; set; }
public System.DateTime AnnounceDate { get; set; }
public int SubjectId { get; set; }
public int StudentId { get; set; }
public virtual Subject Subject { get; set; }
public virtual Student Student { get; set; }
}
I think it's a reference looping error, but I can't realize the approach on how to resolve it. I'm using Web API and I'm able to change the model, so feel free to modify it please. Could this be resolved using FluentAPI?
Here is the exception. It is thrown the first time I have executed the application:
'Introducing FOREIGN KEY constraint 'FK_dbo.Students_dbo.Courses_CourseId' on table 'Students' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.'
You have to use Fluent-API to disable delete / update. To do this, modify the OnModelCreating method:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Course>().HasMany(c => c.Student).WillCascadeOnDelete(false);
}
I'm not sure if it's the Course class or the Student class who causes issues, if this is not working, try to do a "WillCascadeOnDelete(false)" on your Student class.

Categories

Resources