Entity Framework One-to-Many AND a One-To-One relationship - c#

I currently have a data model where a property can have multiple property images:
public class Property
{
public int ID { get; set; }
public string Name { get; set; }
public Guid PrimaryImageID { get; set; }
public ICollection<PropertyImage> Images { get; set; }
}
public class PropertyImage
{
[Key]
public Guid ID { get; set; }
public int PropertyID { get; set; }
public virtual Property Property { get; set; }
}
However, as you can see, i also want to enable a relationship so that a property can have ONE of those images assigned as a primary image.
I found an article here, that seems to use the Fluent API to configure it, but that's all fairly new to me, so i was wondering if it was possible to do this purely using Entity Framework?
What i REALLY want to achieve, is so that i can just call...
property.primaryimage.url
for example. If a user then wanted to change the primage image of a property, then i just change the PrimaryImageId field to the Guid of a different image
Many thanks

Personally, I wouldn't be messing around with EF to do this, the answer in the link you shared would pretty much agree with me. I would simply add another field to the PropertyImage class
public bool IsPrimaryImage {get;set;}
and just find the image based on the value set in that.
Sometimes the simplest solution is the best. You could end up with a convoluted solution in EF that does what you want but at the end of the day, would it really be better than just assigning true or false to a field?

First, you will add a "PrimaryImage" property to your Property class:
public class Property
{
public int ID { get; set; }
public string Name { get; set; }
public Guid PrimaryImageID { get; set; }
public virtual PropertyImage PrimaryImage { get; set; }
public ICollection<PropertyImage> Images { get; set; }
}
In your class where you inherit Entity's framwork DbContext, you can override the method OnModelCreating, which will lead you to:
protected override OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
Then, after the line base.OnModelCreating(modelBuilder), you can write:
modelBuilder.Entity<Property>().
.HasRequired(x => x.PrimaryImage)
.WithRequiredPrincipal();
modelBuilder.Entity<Property>().
.HasMany(x => x.Images)
.WithRequired(x => x.Property);
If this is what you want, then I believe this code allows you to have this property you need. Hope it helps!

If you need a Property to have a primary PropertyImage that can only be an image that is applicable to that Property, the emphasis needs to be switched:
You cannot set the primary Image for a Property until the images are entered and related to the Property to begin with.
You can't add the images unless the Property exists to relate to.
So, you would need to have the PrimaryImage property nullable until later set.
While a PropertyImage relies on a Property, a Property does not rely on a PropertyImage, and so should not be a foreign key in it's record.
This means that the flag (boolean value) for PrimaryImage needs to be stored with the PropertyImage indicating which one of the images is the primary one.
Remove the PrimaryImageId from Property and place a property on the PropertyImage (IsPrimaryImage) to allow selection of the primary one.
You can handle the unique selection either via the UI or more properly with a Unique Constraint on the table.
public class Property
{
public int ID { get; set; }
public string Name { get; set; }
public ICollection<PropertyImage> Images { get; set; }
}
public class PropertyImage
{
[Key]
public Guid ID { get; set; }
public int PropertyID { get; set; }
public bool IsPrimaryImage { get;set; }
public virtual Property Property { get; set; }
}
It isn't good practice to try to structure the data and its relationships around the way you'd like to call a method in code.
You can still call the method the way you want and encapsulate any logic you may need inside.
Think along the lines of if there was a cascade delete applicable here to remove items that no longer have a parent item to relate to:
If you delete a Property, all related PropertyImages would be removed too - correctly so because they relied on that record existing.
If you delete the primary PropertyImage, then the Property would have to be deleted because the record it relates to no longer exists...
So to have your method call the way you would like, do something similar to this:
private void UpdatePrimaryImage(PropertyImage oldImage, PropertyImage newImage)
{
// Pass in the original primary PropertyImage and the new one obtained from the UI.
// Check that we do not have the same image, otherwise no change needs to be made:
if(oldImage.IsPrimary != newImage.IsPrimary)
{
oldImage.IsPrimary = false;
newImage.IsPrimary = true;
Update(oldImage);
Update(newImage);
SaveChanges;
}
}
And to retrieve the current primary image:
Property.PropertyImages.Where(p => p.IsPrimaryImage).Url

Try this:
public class Property
{
public int ID { get; set; }
public string Name { get; set; }
public Guid PrimaryImageID { get; set; }
[ForeignKey("PrimaryImageID")]
public virtual PropertyImage PrimaryImage { get; set; }
public ICollection<PropertyImage> Images { get; set; }
}
public class PropertyImage
{
[Key]
public Guid ID { get; set; }
public int PropertyID { get; set; }
[ForeignKey("PropertyID")]
public virtual Property Property { get; set; }
}

Related

.NET C#: How to dislay foreign key property?

I have this model called Station and ServiceLevel.
Station contains a foreign key called ServiceLevelId which refers to a primary key in the ServiceLevel table.
But I can't show the ServiceLevel properties which is a part of the Station.
I can present the station properties in the view by writing:
#Html.DisplayFor(model => model.Name)
But it won't show the foreign object properties if I write:
#Html.DisplayFor(model => model.ServiceLevel.Title)
I would really appreciate if somebody could explain me why it doesn't work.
Station.cs and ServiceLevel.cs:
public class Station
{
public int StationId { get; set; }
public int Number { get; set; }
public string Name { get; set; }
public int? ServiceLevelId { get; set; }
[ForeignKey("ServiceLevelId")]
public ServiceLevel ServiceLevel { get; set; }
}
public class ServiceLevel
{
public int ServiceLevelId { get; set; }
[Required]
public string Title { get; set; }
}
Merry christmas!
Your ServiceLevel property is not virtual, so EF lazy loading won't work. You have to explicity load the ServiceLevel when querying for Station object, by using Include() method. This is called Eager Loading.
context.Stations.Include(x=>x.ServiceLevel).ToList();
In order for this to work you can make ServiceLevel virtual by prefixing it with 'virtual' keyword. This will enable run time creation of a proxy to your POCO class so it will be available when you call for it;
public class Station
{
public int StationId { get; set; }
public int Number { get; set; }
public string Name { get; set; }
public int? ServiceLevelId { get; set; }
[ForeignKey("ServiceLevelId")]
public virtual ServiceLevel ServiceLevel { get; set; }
}
By default your related entity is not loaded if you don't prefix it by 'virtual' keyword so your code fails.
The other way, as previous answer suggested is to Eager load it, i.e. force the related entity to be loaded. In my humble opinion this is not optimal way to go except you have some specific reason to do so.

EF Code First - How do you specify foreign key name for child object table used by different type of parents

I've got some objects that look like this:
abstract public class Field
{
public int Id { get; set; }
public string Name { get; set; }
public int Ordinal { get; set; }
}
[Table("DropDownField")]
public class DropDownField : Field
{
public virtual List<FieldOption> Options { get; set; }
}
[Table("RadioButtonField")]
public class RadioButtonField : Field
{
public virtual List<FieldOption> Options { get; set; }
}
public class FieldOption
{
public int Id { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
In my database, it ends up creating the a FieldOptions table using Code First. However, it creates the following columns:
Id
Name
Value
DropDownField_Id
RadioButtonField_Id
What I'd like to see is just one Field_Id in this table since the Id of a field has to be unique across the different types of fields.
Is there a way to do this? I've done some searching but I must not know the right search terms to use to find the answer.
imho, what you want, from a relational database point of view, is a column (Option.FieldId) being a foreign key to 2 tables DropDownField and RadioButtonField.
That is whenever you insert an option, FieldId must reference an existing DropDownField AND an existing RadioButtonField.
That is at least weird.
I don't think this can/should be achieved.

How to make proper code-first relations

I'm fairly new to Entity Framework and feel more in control using the Code-First pattern rather than DB-First.
I was wondering what is more preferred when it comes to programmatically setting up ForeignKey relations between the entities.
Is it better to declare a FK_ property in the class which relates to the another class or is it better to declare an IEnumerable<> property in the class that gets related to?
public class IRelateToAnotherClass
{
...
public int FK_IGetRelatedToByAnotherClass_ID { get; set; }
}
or
public class IGetRelatedToByAnotherClass
{
...
public IEnumerable<IRelateToAnotherClass> RelatedTo { get; set; }
}
It all depends on what type of relationships you want between your entities (one-to-one, one-to-many, many-to-many); but, yes, you should declare foreign key properties. Check out this site for some examples.
Here's a one-to-many for your two classes:
public class IRelateToAnotherClass
{
public int Id { get; set; } // primary key
public virtual ICollection<IGetRelatedToByAnotherClass> IGetRelatedToByAnotherClasses { get; set; }
}
public class IGetRelatedToByAnotherClass
{
public int Id { get; set; } // primary key
public int IRelateToAnotherClassId { get; set; } // foreign key
public virtual IRelateToAnotherClass IRelateToAnotherClass { get; set; }
}
and with some Fluent API mapping:
modelBuilder.Entity<IGetRelatedToByAnotherClass>.HasRequired<IRelateToAnotherClass>(p => p.IRelateToAnotherClass).WithMany(p => p.IGetRelatedToByAnotherClasses).HasForeignKey(p => p.Id);
If I understand what you're asking correctly, you'd want both. You want an int FK property and an object property to use as the navigation property.
The end result would look something like this:
public class Employee
{
[Key]
public int EmployeeID { get; set; }
[ForeignKey("Store")]
public int StoreNumber { get; set; }
// Navigation Properties
public virtual Store Store { get; set; }
}
public class Store
{
[Key]
public int StoreNumber { get; set; }
// Navigation Properties
public virtual List<Employee> Employees { get; set; }
}
If you haven't already, take a look at navigation properties and lazy-loading. Note that EF is clever enough to figure out that an int StoreID property corresponds to an object Store property, but if they are named differently (such as without the ID suffix), you must use the [ForeignKey] annotation.

What is the best way to set navigation property having only id in Entity Framework?

I have the next enitities:
public class MyEntity
{
public virtual int Id { get; set; }
public virtual MySecondEntity SecondEntity { get; set; }
}
public class MySecondEntity
{
public virtual int Id { get; set; }
...
some properties here
...
}
I don't want to create MySecondEntityID property in order to have clean model.
I need to set myEntity.MySecondEntity by its Id, but I don't want to request MySecondEntity from DB.
Is it possible to do it without creating MyEntity.MySecondEntityID property and without loading the whole MySecondEntity object or even without any db requests?
It is possible to create the MySecondEntity object manually, and attach it to the context as an unchanged object.
var secondEntity = new MySecondEntity();
secondEntity.Id = id;
context.MySecondEntities.Attach(secondEntity);
entity.SecondEntity = secondEntity;
To keep it simple, I have ignored the possibility that a MySecondEntity object with the same key already exists in the context. To make that work, you should check the local entities (for example by searching context.MySecondEntities.Local) and re-use an entity if you find it in there.
Note: EF won't have any way of knowing that the other properties of secondEntity, which are left at their default values, don't correspond to what's in the database. For what you're doing now, that doesn't matter, but it may matter at a later stage.
Use a mutator method to modify a protected property for the ID:
public class MyEntity
{
public virtual int Id { get; set; }
public virtual MySecondEntity SecondEntity { get; set; }
[ForeignKey( "SecondEntity" )]
protected virtual int? MySecondEntityId { get; set; }
public void SetSecondEntityId( int? id )
{
MySecondEntityId = id;
}
}
For entity Framework 5 or later you can add an Id next to a navigational property, which will be auto-populated when the entity is loaded from the db.
public class MyEntity
{
public int Id { get; set;}
public int MySecondEntityId { get; set;}
public virtual MySecondEntity MySecondEntity { get; set;}
}
Setting MySecondEntityId is enough to create the relationship (after saving). This way you don't have to load the actual entity from db.
If you have a nullable foreign key, the making the navigational Id nullable is enough.
public int? MySecondEntityId { get; set;}
If you want to get the MyEntity object from your database without initializing the MySecondEntity object, you can turn off lazy loading.
Method one
Turn of lazy loading on your context.
Method two
Remove the virtual keyword from your entity:
public class MyEntity
{
public virtual int Id { get; set; }
public MySecondEntity SecondEntity { get; set; }
}
This will disable lazy loading for SecondEntity.
More info.
Finally found a solution for this problem of keeping our models as clean as possible: Shadow foreign key.
I had a bit more complex case and got it working.
Model
// main class that is aware of the other
public sealed class Foo
{
public int Id { get; set; }
public IEnumerable<FooBar> FooBars { get; set; }
// ... other props
}
// class that associates Foos and Bars with some extra props (not tied to Foos or Bars specifically). Goal here is to not have any ID props
public sealed class FooBar
{
public Foo Foo { get; set; }
public Bar Bar { get; set; }
public bool Enabled { get; set; }
}
// Bar doesn't even know Foo exists
public sealed class Bar
{
public int Id { get; set; }
// ... other props
}
In my case I have a many to many relationship, so the primary key of FooBar is a composite key of the two main table IDs.
OnModelCreating:
modelBuilder.Entity<FooBar>(b =>
{
const string FooId = "FooId";
const string BarId = "BarId";
b.Property<int>(FooId);
b.Property<int>(BarId);
b.HasKey(FooId, BarId);
});
Conventions that care of the rest.

EF code first and virtual properties

I have two tables Articles and Events and I'd like to provide a commenting functionality to the users on both types. The hard part is that I'd like to use a navigation property that returns the comments belonging to the given EF object.
public class Article
{
public virtual ICollection<Comment> Comments { get; set; }
/* more properties here */
}
public class Event
{
public virtual ICollection<Comment> Comments { get; set; }
/* more properties here */
}
public class Comment
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CommentId { get; set; }
public string Msg { get; set; }
public DateTime SentAt { get; set; }
public int TargetId { get; set; }
public CommentTargeType TargetType { get; set; }
}
public enum CommentTargeType
{
Article,
Event
}
As you see the TargetId would be the id of the Article or of the Event and the TargetType is to distinguish these two types.
So, is there any way to do this? Or would it be better to create an ArticleComments and an EventComments type instead?
Your current design is essentially using the same field in your object to be a foreign key into 2 tables. I would advise against that because the database won't be able to force any constraints or do integrity checks.
You can add two int? fields, one called ArticleId and one called EventId to accomplish what you want. Since the types are int? they will be nullable fields in the database.
I would even go one step farther and use the ForeignKey attribute so that EntityFramework knows about this and creates the foreign keys for you.
[ForeignKey("Article")]
public int? ArticleId { ... }
public virtual Article Article { get; set; }
[ForeignKey("Event")]
public int? EventId { get; set; }
public virtual Event Event { get; set; }

Categories