EF Code First Flexible Relationships - c#

I'm trying to set up a flexible one-to-many relationship in EF. Imagine we have the following POCO classes:
public class EntityA
{
public Guid Id { get; set; }
public virtual IEnumerable<Event> Events { get; set; }
}
public class EntityB
{
public Guid Id { get; set; }
public virtual IEnumerable<Event> Events { get; set; }
}
public class Event
{
public Guid Id { get; set; }
public Guid EntityId { get; set; }
}
I want the Event class to link to either Entity through the EntityId propery. So it's not a normal One-To-Many relationship.
I don't need any relationship properties on the Event side, but I would like there to be an IEnumerable<Event> Events property in each of the two Entity classes, which will allow me to grab a list of linked events for any entity I choose.
I'm using EntityTypeConfiguration<T> maps but I need help working out the relationships on this.

Related

How can I eager load objects that are referencing the primary key of my POCO class in a 1:Many relationship?

Considering the documentation here, you can define foreign key relationships in your pocos like the given example:
public class Customer
{
[References(typeof(CustomerAddress))]
public int PrimaryAddressId { get; set; }
[Reference]
public CustomerAddress PrimaryAddress { get; set; }
}
This is fine, as there's a 1:1 relationship here. However, I have a 1:Many relationship I need to define, and the relationship is actually defined in the child object, not the parent object.
So, let's say I have these POCOs:
public class Customer
{
[PrimaryKey]
public int CustomerId { get; set; }
public List<CustomerAddress> CustomerAddresses { get; set; }
}
public class CustomerAddress
{
[PrimaryKey]
public int CustomerAddressId{ get; set; }
public int CustomerId { get; set; }
}
How can I have ORMLite eager load the CustomerAddresses property in the Customer POCO?
You have to call Db.LoadSelect<Customer>() method and your customer(s) will retrieve CustomerAddresses (you need to add [Reference] attribute on top of your CustomerAddresses property).

EF Code First 1:0..1 relationship error: Multiplicity is not valid in Role 'EFEmployee_Identity_Source' in relationship 'EFEmployee_Identity'

Full error:
One or more validation errors were detected during model generation:
EFEmployee_Identity_Source: : Multiplicity is not valid in Role 'EFEmployee_Identity_Source' in relationship 'EFEmployee_Identity'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
I am dealing with three types of entities: EFEmployee, EFPerson, and EFOffice. It's kind of weird that I'm getting this error because the code I'm testing only creates an instance of an EFOffice entity. Anyway, here is the EFEmployee entity class:
[Table("employee_entity")]
public class EFEmployee : EFBusinessEntity
{
[ForeignKey("Office")]
public Guid OfficeID { get; set; }
[ForeignKey("Identity")]
public Guid PersonID { get; set; }
[Column("hire_date")]
public DateTime HireDate { get; set; }
[Column("job_title")]
public byte[] JobTitle { get; set; }
[Column("salary")]
public int Salary { get; set; }
[Column("certifications")]
public byte[] Certifications { get; set; }
[Column("vacation_time")]
public int VacationTime { get; set; }
[Column("sick_time")]
public int SickTime { get; set; }
public virtual EFOffice Office { get; set; }
public EFPerson Identity { get; set; }
public virtual EFEmployee ReportingTo { get; set; }
}
And this is my EFPerson entity class:
[Table("person_entity")]
public class EFPerson : EFBusinessEntity
{
[Column("first_name")]
[StringLength(50)]
public string FirstName { get; set; }
[Column("last_name")]
[StringLength(50)]
public string LastName { get; set; }
[Column("phone_num")]
public uint? PhoneNum { get; set; }
[Column("date_of_birth")]
public DateTime DateOfBirth { get; set; }
public EFEmployee Employee { get; set; }
}
You can see that they both inherit from EFBusinessEntity, which is here:
[Table("business_entity")]
public abstract class EFBusinessEntity : IBusinessEntity
{
[Column("tenant_id")]
public Guid TenantId
{
get;
set;
}
[Column("id")]
[Key]
public Guid Id
{
get;
set;
}
}
As you can see, there is a one-to-zero-or-one relationship between EFEmployee and EFPerson, with EFEmployee being the dependent side since there can be a person who is not an employee, but there can't be an employee who is not a person too. Since EFEmployee is the dependent side, I have added a PersonID in EFEmployee with the data annotation (attribute?) above denoting that it's the foreign key to Person:
[ForeignKey("Identity")]
public Guid PersonID { get; set; }
I think I've made it pretty clear for Entity Framework that this is a 1:0..1 relationship. Does anyone know how to solve this error using data annotations (or attributes, whatever those square bracket things above properties are). I can't use fluent API for reasons I'm not getting into.
Generally, with 1:0..1 relationships in Entity Framework, the dependent side needs to use its primary key as the foreign key. Fortunately, for your case, this doesn't seem like it would be a bad idea. You would need to:
Remove the EFEmployee.PersonID property
Add [ForeignKey("Id")] to EFEmployee.Identity
Edit: May not work because key and navigation property are on separate classes. See this.
Having EFEmployee inherit from EFPerson seems like it might be a viable option as well. Inheritance uses TPH by default, but if you want to use TPT (table-per-type), add the [Table] attribute to your type.
I did some more playing around with the models and found out what was wrong. So I kept the foreign key attribute with EFPerson.Identity like jjj suggested:
[ForeignKey("PersonID")]
public virtual EFPerson Identity { get; set; }
Then the other change I had to make was in the EFPerson class. In my EFPerson class I had the navigation property to EFEmployee:
public virtual EFEmployee Employee { get; set; }
However, since this is a 1:0..1 relationship with EFEmployee being the dependent side (i.e. the non-essential side), I removed that navigation property, and when I ran my test it worked.

MVC3 Entity Framework many-to-many with additional column

I'm new to asp.net, mvc3 and entity framework.
I'm trying to develop a mvc3 programm with entity framework and code-first.
So I have two classes with a many-to-many relationship.
One class called "User" the other one is "Course".
public class Course : IValidatableObject
{
[...]
public virtual ICollection<User> Users { get; set; }
[...]
}
public class User : IValidatableObject
{
[...]
public virtual ICollection<Course> Courses { get; set; }
[...]
}
So, this works. But now I need an additional field which safes the status of the registration for a course.
Is there an easy way I don't know?
I tried it this way:
public class Course : IValidatableObject
{
[...]
public virtual ICollection<CourseUser> CourseUsers { get; set; }
[...]
}
public class User : IValidatableObject
{
[...]
public virtual ICollection<CourseUser> CourseUsers { get; set; }
[...]
}
public class CourseUser
{
[Key, ForeignKey("Course"), Column(Order = 0)]
public int Course_ID { get; set; }
[Key, ForeignKey("User"), Column(Order = 1)]
public string User_ID { get; set; }
public int Status { get; set; } //{ pending, approved }
public virtual Course Course { get; set; }
public virtual User User { get; set; }
}
But this makes it much more difficult to add or edit related data.
For example I didn't managed it yet to automatically add the user who created the course to the CourseUsers table.
No there is no easier way to do that. Once you add any additional field to your junction table it must be mapped as entity to allow you access to that field. It is not pure many-to-many relation any more. It is a new entity in your model with two one-to-many relations.

EF Code First Many to many relation store additional data in link table

How do I store additional fields in the "link table" that is automagically created for me if I have two entities associated as having a many to many relationship?
I have tried going the "two 1 to many associations"-route, but I'm having a hard time with correctly configuring the cascading deletion.
Unless those extra columns are used by some functions or procedures at the database level, the extra columns in the link table will be useless since they are completely invisible at the Entity Framework level.
It sounds like you need to re-think your object model. If you absolutely need those columns, you can always add them later manually.
You will most likely need to expose the association in your domain model.
As an example, I needed to store an index (display order) against items in an many-to-many relationship (Project <> Images).
Here's the association class:
public class ProjectImage : Entity
{
public Guid ProjectId { get; set; }
public Guid ImageId { get; set; }
public virtual int DisplayIndex { get; set; }
public virtual Project Project { get; set; }
public virtual Image Image { get; set; }
}
Here's the mapping:
public class ProjectImageMap : EntityTypeConfiguration<ProjectImage>
{
public ProjectImageMap()
{
ToTable("ProjectImages");
HasKey(pi => pi.Id);
HasRequired(pi => pi.Project);
HasRequired(pi => pi.Image);
}
}
From Project Map:
HasMany(p => p.ProjectImages).WithRequired(pi => pi.Project);
Maps to the following property on project:
public virtual IList<ProjectImage> ProjectImages { get; set; }
Hope that helps
Ben
Suppose there is a many-to-many association between two types: User and Message, and the association class is defined as UserMessageLink with additional properties.
public class User {
public int Id {get;set;}
}
public class Message {
public int Id {get;set;}
}
//The many-to-many association class with additional properties
public class UserMessageLink {
[Key]
[Column("RecieverId", Order = 0)]
[ForeignKey("Reciever")]
public virtual int RecieverId { get; set; }
[Key]
[Column("MessageId", Order = 1)]
[ForeignKey("Message")]
public virtual int MessageId { get; set; }
public virtual User Reciever { get; set; }
public virtual Message Message { get; set; }
//This is an additional property
public bool IsRead { get; set; }
}

Code First CTP: Multiple PKs or FKs

In some instances, we may have PK/FK references duplicated, so entity A and entity B are in a PK/FK relationship, but 3 times. So entity A would have 3 FK collections and entity B would have 3 entity references. How does that work with the code-first template? Do you follow the naming convention of Entity Framework model/database first approaches (EntityA, EntityA1, etc.), and it knows how to hook those relationships up, or is there an extra step, or what?
Thanks.
If you were to define your classes like this
public class EntityA
{
public int EntityAId { get; set; }
public virtual EntityB EntityB1 { get; set; }
public virtual EntityB EntityB2 { get; set; }
public virtual EntityB EntityB3 { get; set; }
}
public class EntityB
{
public int EntityBId { get; set; }
public string Name { get; set; }
}
EF Code First would create two tables (EntityAs, EntityBs). By convection the EntityAs table would have a primary key of EntityAId and three foreign keys linking to EntityB called (EntityB1_EntityBId, EntityB2_EntityBId, EntityB3_EntityBId).
You can however override this convection by adding properties for the foreign keys and adding RelatedTo tags on the navigation properties.
For example:
public class EntityA
{
public int EntityAId { get; set; }
public int MySpecialFkName { get; set; }
[RelatedTo(ForeignKey = "MySpecialFkName")]
public EntityB EntityB1 { get; set; }
}
If you didn't want the RelatedTo meta data in your POCO class, you could instead define the relationship in the OnModelCreating method.
modelBuilder.Entity<EntityA>().HasRequired(p => p.EntityB1)
.HasConstraint((fk, pk) => fk.MySpecialFkName == pk.EntityBId);

Categories

Resources