when working with my POCO objects, I prefer to delay instantiation of navigation properties as much as possible (performance reasons). Instead I'd like to use the FK Value on my entities.
For Example:
class Car
{
public int Id {get;set;}
public int RegistrationId {get;set;}
public virtual Registration Registrations {get;set;} // Nav Property
}
class Registration
{
public int Id {get;set;}
public int CarId {get;set;}
public virtual Car Car {get;set;} // Nav Property
}
Given this is a 1:1 Relationship, how can I get EF to populate my FK Values for me? (using EF 6.0.1)
Related
I like to reference to a specific entry in a database. Is there a data annotation, which I could use
E.g.
public class Address
{
public int CityId {get; set;}
}
public class City
{
public int id {get; set;}
}
So the Address.CityId references the City.id.
And how can I reference to Table Columns via data annotations.
Please refer this link .
https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application
in that read Topic : The Key Attribute
What you want is called One-To-One entity mapping. You can create navigation property in your Address class as follows:
public class Address
{
[Key]
public int AddressID {get;set;}
public int CityID {get; set;}
[ForeignKey("CityID")]
public City City {get;set;}
}
public class City
{
[Key]
public int CityID {get; set;}
}
Add ForeignKey attribute to your navigation property to create mapping through foreign key. And do not forget to define key attributes in each EF model.
More details on Entity Framework home page: Configure One-to-Zero-or-One Relationship
We are using Entity Framework and We have one unique requirement, and after trying many possible options, I couldn't figure out how I can do that, below is the problem summary.
I have following Entity
public class SuperParent
{
[Key]
public int SupoerParentId {get;set;}
public virtual ICollection<Parent> Intermediates {get;set;}
}
public class Parent
{
[Key]
public int ParentId {get;set;}
[ForeignKey("SuperParentId")]
public virtual SuperParent Ancestor {get;set;}
public int SuperParentId {get;set;}
public virtual ICollection<Child> Children {get;set;}
}
public class Child
{
[Key]
public int ChildId {get;set;}
[ForeignKey("ParentId")]
public virtual Parent Ancestor {get;set;}
public int ParentId {get;set;}
/// Area of guidance required here..............
/// I just want to some what denormalize table and add SuperParentId also in
/// Child and in Database. As most of time its child we query and its very
/// efficient for us to directly query based on SuperParentId, I want to do
/// something like below:
[ForeignKey("SuperParentId")]
public virtual SuperParent Ancestor {get;set;}
public int SuperParentId SuperAncestorId {get;set;}
}
We have 1:N:N relationship, and many times we just want to bypass Parent and from SuperParent to directly want to reach to Child... Currently multi level joins are having problem and our query are not efficient, we store large amount of data and each table has 20+ columns.
Questions:
Is it possible with EF? then how I can write modelBinder
OnModelCreating to support this?
Any other alternative?
If you want to have that kind of design, then by default Child will have cascade delete from Parent and SuperParent, which is not allowed in sql.
public int SuperParentId { get; set; } // -> non nullable
If a foreign key on the dependent entity is not nullable, then Code
First sets cascade delete on the relationship. Source
You can simply remove the default cascade delete from SuperParent.
modelBuilder.Entity<Child>()
.HasRequired(c => c.Ancestor)
.WithMany()
.WillCascadeOnDelete(false);
If you have Child collection on SuperParent, mention it when calling WithMany.
public DbSet<Child> Children { get; set; }
Change above WithMany with
.WithMany(sp => sp.Children)
I'm using EF Code first.
I created two classes. For simplicity, imagine that I have a User table (class) and a FileAttachment table. I want to use the FileAttachment table with many other classes, so that any part of the application that requires having a FileAttachment can reuse that table. The problem is that when EF generates the schema, it creates a Foreign Key in the FileAttachment table back to User table. Is there a way to disable that?
Thanks
You need to build an intermediate class.
public class UserDocument
{
public int Id {get;set;}
public int UserId {get;set;}
public virtual User User {get;set;}
public int FileAttachmentId {get;set;}
public virtual FileAttachment FileAttachment {get;set;}
}
So your user class can now have:
public virtual ICollection<UserDocument> Documents {get;set;}
And in this case, FileAttachment class will not have reference to User.
If you want now to build some other document type, just implement another intermediate type, i.e. imagine you want to have CustomerDocument:
public class CustomerDocument
{
public int Id {get;set;}
public int CustomerId {get;set;}
public virtual Customer Customer {get;set;}
public int FileAttachmentId {get;set;}
public virtual FileAttachment FileAttachment {get;set;}
}
And then your hypothetical Customer class would have:
public virtual ICollection<CustomerDocument> Documents {get;set;}
even if I done googlin' about that for more than hour, anyone knows if
in EF5 a Complex Types can participate in an association?
I know till EF4.x that wasn't possible, but now? there's an official documentation statement about that?
My need is mapping a class Person with Address Complex Types which refer a City
public class Person
{
public int ID {get;set;}
public string Name {get;set;}
public Address Address {get; set;}
}
public class Address
{
public string Street {get; set;}
public City City {get; set;}
}
public class City
{
public int ID {get;set;}
public string Name {get;set;}
}
I need to do a mapping with EntityTypeConfiguration<T> specifying the Complex Type association. It's that possible in EF5?
Thanks in advance
EDIT:
I saw that link complex type even if it refer to Entity Data Model, it says that it's not possible to map associations to Complex Types. It's that still true?
Even as of Entity Framework 6, complex types cannot contain navigation properties.
Let's take a simple example. Suppose I had the following:
public class Category
{
public int CategoryID {get;set;}
}
public class Product
{
public int ProductID {get;set;}
public int CategoryID {get;set;}
public Category Category {get;set;}
}
EF
modelBuilder.Entity<Product>().HasRequired(req=>req.Category).WithMany().Map(m=>m.MapKey("CategoryID"));
Do I have to always 'include' categories simply to get the CategoryID assigned to my product? The SQL it generates without an .Include (e.g. db.Products.All()) will in fact return the CategoryID.
You need .Include to populate the category nav property (otherwise it will be null). the CategoryID property will always be populated