issue in many to many relationship entity framework code first method - entity-framework

I have created an application in entityframework using code first method.
In application , there are two entities which have many to many relationship between them.
public class Course
{
[Key]
public int CourseId { get; set; }
public string Name { get; set; }
public ICollection<Student> Student{ get; set; }
}
public class Student
{
[Key]
public int StudentId { get; set; }
public string Name { get; set; }
public ICollection<Course> Course{ get; set; }
}
when I execute the project, it creates student table,Course table,StudentCourse table.
Now now the problem is,in StudentCourse table there are only two keys, StudentID and CourseId
I want to add additional column in that table how to do that ?

Declare a class defining that table:
public class Course
{
[Key]
public int CourseId { get; set; }
public string Name { get; set; }
public ICollection<Student> Student{ get; set; }
}
public class Student
{
[Key]
public int StudentId { get; set; }
public string Name { get; set; }
public ICollection<Course> Course{ get; set; }
}
public class StudentCourse
{
public int StudentId { get; set; }
public int CourseId { get; set; }
//More columns here
}

Related

ForeignKey with a list of ApplicationUser objects

When it comes to a single object's foreign key, we would have a property:
SingleMap.cs
[ForeignKey("ApplicationUser")]
public int ApplicationUserID { get; set; }
public ApplicationUser ApplicationUser { get; set; }
How do we map the foreignkey when we have a list of ApplicationUsers?
ManyMap.cs
public ICollection<ApplicationUser> ApplicationUserList { get; set; }
it is usually used this way:
public class ManyMap
{
public int ID {get; set;}
.....
[InverseProperty(nameof(ApplicatonUser.ManyMap))]
public ICollection<ApplicationUser> ApplicationUsers{ get; set; }
}
public class ApplicationUser
{
......
public int ManyMapID { get; set; }
[ForeignKey(nameof(ManyMapID))]
[InverseProperty("ApplicationUsers")]
public ManyMap ManyMap { get; set; }
}

EF Core Many-to-many table to another table

I have a question regarding Many to Many tables using EF Core.
Assume I have the below situation:
public class Book
{
public int BookId { get; set; }
public string Title { get; set; }
public Author Author { get; set; }
public ICollection<BookCategory> BookCategories { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public ICollection<BookCategory> BookCategories { get; set; }
}
public class BookCategory
{
public int BookId { get; set; }
public Book Book { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
Now, assume that I want to have another table called "Contracts" which should be a child of "BookCategory".
How can I create this relationship?

EF Core code first Inheritance of separate table

Say I have a table Company defined in following entity:
public class Company
{
public Guid CompanyId { get; set; }
[Required]
public string Name { get; set; }
[MaxLength(50)]
public string Uid { get; set; }
...
}
And I need another table CompanyHistory what will have all fields of Company extended with CompanyHistoryId, EffectiveDate, DEffectiveDate.
I have tried it like this:
public class CompanyHistory : Company
{
public Guid CompanyHistoryId { get; set; }
public virtual Company { get; set; }
}
But instead of 2 tables migration makes one and combines all the columns.
How can I get same result without writing all the column again as is done here:
public class CompanyHistory
{
public Guid CompanyHistoryId { get; set; }
public Guid CompanyId { get; set; }
public virtual Company Company { get; set; }
[Required]
public string Name { get; set; }
[MaxLength(50)]
public string Uid { get; set; }
...
}

Entity Framework Code first adds unwanted foreign key column

I have a many to one relationship between Categories and News.
The problem I'm having is that EF keeps adding a foreign key colum to my table which I dont want!
News class
public class News
{
public News()
{
}
[Key]
public int NewsID { get; set; }
public int PublishedByID { get; set; }
public string PublishedByFullName { get; set; }
public string PublishedByEmail { get; set; }
public DateTime DatePublished { get; set; }
public string Title { get; set; }
public string PreviewText { get; set; }
public string BlobName { get; set; }
public virtual Category Category { get; set; }
}
Categories class
public class Category
{
public Category()
{
News = new HashSet<News>();
}
[Key]
public int CategoryID { get; set; }
public string Name { get; set; }
public string CategoryTypeName { get; set; }
public virtual ICollection<News> News { get; set; }
}
Database
My question
How do I remove Category_CategoryID in News table?
I'm guessing i'm missing some code in my OnModelCreating method.
You need to add id field in News class to reference Category.
public class News
{
[Key]
public int NewsID { get; set; }
public int CategoryID { get; set; } // added
public int PublishedByID { get; set; }
public string PublishedByFullName { get; set; }
public string PublishedByEmail { get; set; }
public DateTime DatePublished { get; set; }
public string Title { get; set; }
public string PreviewText { get; set; }
public string BlobName { get; set; }
public virtual Category Category { get; set; }
}

EF5 CodeFirst + Relationships between non-key columns

In have two classes;
public class Item
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual BinCard BinCard { get; set; }
}
and
public class BinCard
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public int ItemId { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
public double Qty { get; set; }
public ObservableCollection<Item> Item { get; set; }
}
The BinCard.Id is the PK and auto increments.
I want the relationship between two tables using Item.Id and BinCard.ItemId using*FluentAPI*.
Please help me to create this relationship correctly.
EF does not support non-PK principal Ids because it doesn't support unique keys other than PKs yet. To make this work you will have to create relationship based on BinCard.Id and BinCardId to Item entity - btw. it looks like the correct way to build relationship in your model. Your current model looks really strange.
Thanks for all replies. This is the solution for my question.
public class BinCard
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public int ItemId { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
public double Qty { get; set; }
public virtual Drug Drug { get; set; }
}
public class Item
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public ObservableCollection<BinCard> BinCard { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Drug>()
.HasRequired(s => s.Stock)
.WithRequiredPrincipal(s => s.Drug);
}