EF Code First Additional column in join table for ordering purposes - entity-framework-4.1

I have two entities that have a relationship for which I create a join table
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Image> Images { get; set; }
}
public class Image
{
public int Id { get; set; }
public string Filename { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>()
.HasMany(i => i.Images)
.WithMany(s => s.Students)
.Map(m => m.ToTable("StudentImages"));
}
I would like to add an additional column to allow chronological ordering of the StudentImages.
Where should I add insert the relevant code?

Do you want to use that new column in your application? In such case you cannot do that with your model. Many-to-many relation works only if junction table doesn't contain anything else than foreign keys to main tables. Once you add additional column exposed to your application, the junction table becomes entity as any other = you need third class. Your model should look like:
public class StudentImage
{
public int StudentId { get; set; }
public int ImageId { get; set; }
public int Order { get; set; }
public virtual Student Student { get; set; }
public virtual Image Image { get; set; }
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<StudentImage> Images { get; set; }
}
public class Image
{
public int Id { get; set; }
public string Filename { get; set; }
public virtual ICollection<StudentImage> Students { get; set; }
}
And your mapping must change as well:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<StudentImages>().HasKey(si => new { si.StudentId, si.ImageId });
// The rest should not be needed - it should be done by conventions
modelBuilder.Entity<Student>()
.HasMany(s => s.Images)
.WithRequired(si => si.Student)
.HasForeignKey(si => si.StudentId);
modelBuilder.Entity<Image>()
.HasMany(s => s.Students)
.WithRequired(si => si.Image)
.HasForeignKey(si => si.ImageId);
}

Related

How to deal with Duplicated variables in EntityFrameWork Core

I need help figuring out how to prevent duplicates of a variable. I am building a blog, and don't want the same tags to be created over and over each time I use it.
class MyContext : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PostTag>()
.HasKey(t => new { t.PostId, t.TagId });
modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Post)
.WithMany(p => p.PostTags)
.HasForeignKey(pt => pt.PostId);
modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Tag)
.WithMany(t => t.PostTags)
.HasForeignKey(pt => pt.TagId);
}
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public List<PostTag> PostTags { get; set; }
}
public class Tag
{
public string TagId { get; set; }
public string Name { get; set; }
public List<PostTag> PostTags { get; set; }
}
public class PostTag
{
public int PostId { get; set; }
public Post Post { get; set; }
public string TagId { get; set; }
public Tag Tag { get; set; }
}
Do I need to create a custom method to make this happen, or is there a way to get it done using Fluent APi?
If the tag already exists, I want to instead add the post to the already existing list of posts with that same tagName.

Mapping table is empty

Below are two tables that has a many to many relation and and also another table that has a relation with the two first. A mapping table is created by Visual Studio with the name OrderEmployee, but when I run the application and enter some information to the Timeunit create form, the mapping table OrderEmployee is empty!
Should there not be some IDs added to that table since it has a relation with the Timeunit table or how is this mapping table thing working, when will there be data added to the mapping table? Or is something wrong with my Entity Classes?
public class Order
{
public int ID { get; set; }
public string Name { get; set; }
public int? ManufacturerID { get; set; }
public virtual Manufacturer Manufacturer { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int EmployeeNumber { get; set; }
public virtual ICollection<Timeunit> Timeunits { get; set; }
public virtual ICollection<Order> Order { get; set; }
}
public class Timeunit
{
public int ID { get; set; }
public int Week { get; set; }
public int HoursPerWeek { get; set; }
public int EmployeeID { get; set; }
public int? OrderID { get; set; }
public virtual Employee Employee { get; set; }
public virtual Order Order { get; set; }
}
EDIT:
Create Method for Timeunit:
public ActionResult Create([Bind(Include = "ID,Week,HoursPerWeek,EmployeeID,OrderID")] Timeunit timeunit)
{
if (ModelState.IsValid)
{
db.Timeunits.Add(timeunit);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ProjectID = new SelectList(db.Orders, "ID", "Name", timeunit.OrderID);
ViewBag.ResourceID = new SelectList(db.Employees, "ID", "Name", timeunit.EmployeeID);
return View(timeunit);
}

EF Fluent Define Foreign primary key

I have defined a set of Code First entities as such:
public class User
{
public int UserId { get; set; }
public string username { get; set; }
}
public abstract class UserType
{
public virtual User User { get; set; }
public int UserId { get; set; }
}
public class RoleUser : UserType
{
public string role { get; set; }
}
public class SiteUser : UserType
{
public string site { get; set; }
}
public class Testctx:DbContext
{
static Testctx()
{
Database.SetInitializer(new DropCreateDatabaseAlways<Testctx>());
}
public DbSet<User> Users { get; set; }
public DbSet<SiteUser> SiteUsers { get; set; }
public DbSet<RoleUser> RoleUsers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().HasKey(u=>u.UserId).ToTable("User");
modelBuilder.Entity<RoleUser>().HasKey(u => u.UserId).ToTable("RoleUser");
modelBuilder.Entity<SiteUser>().HasKey(u => u.UserId).ToTable("SiteUser");
}
}
If I wanted User, RoleUser, SiteUser, to each have a ID column named "ID" How can I achieve this while keeping the foreign primary key relationship to user in Usertype.
For instance if i declare public class Entity{ public int ID {get;set;}} and have all inherit from Entity, I'm left with a UserId and an ID field in RoleUser and SiteUser.

unidirectional many-to-many relationship with Code First Entity Framework

I am new to EF, and trying to get many-to-many unidirectional relationship with code first approach. For example, if I have following two classes (not my real model) with be a N * N relationship between them, but no navigation property from "Customer" side.
public class User {
public int UserId { get; set; }
public string Email { get; set; }
public ICollection TaggedCustomers { get; set; }
}
public class Customer {
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
The mapping code looks like ...
modelBuilder.Entity()
.HasMany(r => r.TaggedCustomers)
.WithMany(c => c.ANavgiationPropertyWhichIDontWant)
.Map(m =>
{
m.MapLeftKey("UserId");
m.MapRightKey("CustomerId");
m.ToTable("BridgeTableForCustomerAndUser");
});
This syntax force me to have "WithMany" for "Customer" entity.
The following url, says "By convention, Code First always interprets a unidirectional relationship as one-to-many."
Is it possible to override it, or should I use any other approach?
Use this:
public class User {
public int UserId { get; set; }
public string Email { get; set; }
// You must use generic collection
public virtual ICollection<Customer> TaggedCustomers { get; set; }
}
public class Customer {
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
And map it with:
modelBuilder.Entity<User>()
.HasMany(r => r.TaggedCustomers)
.WithMany() // No navigation property here
.Map(m =>
{
m.MapLeftKey("UserId");
m.MapRightKey("CustomerId");
m.ToTable("BridgeTableForCustomerAndUser");
});

Entity Framework Code 1st - Mapping many-to-many with extra info

I've looked through several of the questions here and am not quite connecting all the (mental) dots on this. I would appreciate some help.
My Models (code first):
public class cgArmorial
{
[Key]
[Display(Name = "Armorial ID")]
public Guid ArmorialID { get; set; }
[Display(Name = "User ID")]
public Guid UserId { get; set; }
public string Name { get; set; }
public string DeviceUrl { get; set; }
public string Blazon { get; set; }
public virtual ICollection<cgArmorialAward> ArmorialAwards { get; set; }
}
public class cgArmorialAward
{
public cgArmorial Armorial { get; set; }
public cgAward Award { get; set; }
public DateTime AwardedOn { get; set; }
}
public class cgAward
{
[Key]
[Display(Name = "Award ID")]
public Guid AwardID { get; set; }
public string Name { get; set; }
public string Group { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
public string Blazon { get; set; }
public virtual ICollection<cgArmorialAward> ArmorialAwards { get; set; }
}
Then in my Context class I have (last 2 entries):
public class Context : DbContext
{
public DbSet<cgUser> Users { get; set; }
public DbSet<cgEvent> Events { get; set; }
public DbSet<cgEventType> EventTypes { get; set; }
public DbSet<cgArmorial> Armorials { get; set; }
public DbSet<cgAward> Awards { get; set; }
public DbSet<cgArmorialAward> ArmorialAwards { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<cgUser>()
.HasMany<cgEvent>(e => e.EventAutocrats)
.WithMany(u => u.EventAutocrats)
.Map(m =>
{
m.ToTable("EventAutocrats");
m.MapLeftKey("UserId");
m.MapRightKey("EventId");
});
modelBuilder.Entity<cgUser>()
.HasMany<cgEvent>(e => e.EventStaff)
.WithMany(u => u.EventStaff)
.Map(m =>
{
m.ToTable("EventStaff");
m.MapLeftKey("UserId");
m.MapRightKey("EventId");
});
modelBuilder.Entity<cgArmorialAward>()
.HasRequired(a => a.Armorial)
.WithMany(b => b.ArmorialAwards);
modelBuilder.Entity<cgArmorialAward>()
.HasRequired(a => a.Award)
.WithMany(); // b => b.ArmorialAwards
}
}
I am getting this error when I try to run:
System.Data.Edm.EdmEntityType: : EntityType 'cgArmorialAward' has no
key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �ArmorialAwards�
is based on type �cgArmorialAward� that has no keys defined.
Well, as the exception says: You don't have a key defined on your entity cgArmorialAward. Every entity must have a key. Change it to the following:
public class cgArmorialAward
{
[Key, Column(Order = 0)]
[ForeignKey("Armorial")]
public Guid ArmorialID { get; set; }
[Key, Column(Order = 1)]
[ForeignKey("Award")]
public Guid AwardID { get; set; }
public cgArmorial Armorial { get; set; }
public cgAward Award { get; set; }
public DateTime AwardedOn { get; set; }
}
The fields in the composite key are foreign keys to the other two tables at the same time, hence the ForeignKey attribute. (I'm not sure if conventions would detect this automatically because you have non-standard names ("cgXXX" for the classes and "XXXId" for the foreign key properties). On the other hand the property names Armorial and Award match the foreign key property names. I'm not sure if EF conventions would consider this. So, perhaps the ForeignKey attribute is not necessary but at least it's not wrong.)