How to create non-virtual navigation property from EDMX? - entity-framework

By default, all the navigation properties created from EDMX are marked as virtual ICollection<...>.
Is it possible to eager load one of the navigation properties? Like,
public partial class Applicant
{
public int Id { get; set; }
...
public ContactInfo ContactInfo { get; set; }
}
In this way, I will be able to have ContactInfo in the interface.
public interface IApplicant
{
int Id { get; set; }
ContactInfo ContactInfo { get; set; }
}
public partial class Applicant : IApplicant
{
}
Is it possible?

Related

Many-to-Many EF core with ApplicationUser

I am trying to create a many-to-many relationship between ApplicationUser and Reserve.
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
this.Reserves = new HashSet<Reserve>();
}
public bool IsProfessional { get; set; }
public virtual ICollection<Reserve> Reserves { get; set; }
}
public class Reserve
{
public Reserve()
{
this.Usuarios = new HashSet<ApplicationUser>();
}
public int ID { get; set; }
public string Begin { get; set; }
public string End { get; set; }
public string Date { get; set; }
public string Status { get; set; } //free , reserved, confirmed, canceled
public DateTime LastUpdate { get; set; }
public virtual ICollection<ApplicationUser> Usuarios { get; set; }
}
But I cant add the migration
PM> Add-Migration user-reserve-relationship
Unable to determine the relationship represented by navigation property 'ApplicationUser.Reserves' of type 'ICollection<Reserve>'. Either manually configure the relationship, or ignore this property from the model.
Anyway, the same relationship works well to others entities but not ApplicationUser.
Can I use ApplicationUser many-to-many?
I found the follow in ef core :
Many-to-many relationships without an entity class to represent the join table are not yet supported.
So my question is closed.

How do i create One-to-One to relationship without having navigation property in dependent entity

I understand the following code creates "One-to-One relationship" between a principal and a dependent entity.
However, I would like to ask:
Is it possible to create one-to-one relationship without including navigation property in the dependent entity?
If yes, than how should I re-write the following code?
public class Student
{
[Key]
public int Id { get; set; }
public string FullName { get; set; }
public StudentReport StudentReport { get; set; }
}
public class StudentReport
{
[Key, ForeignKey("Student")]
public int Id { get; set; }
public string RollNumber { get; set; }
public string StudentType { get; set; }
public Student Student { get; set; }
}
To create a one-to-one relationship without a navigation property on the dependent side, you'll need to use the fluent API. For example, in your DbContext class, you can override OnModelCreating and use this to define the relationship:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// I'm assuming the report is optional
modelBuilder.Entity<Student>()
.HasOptional(t => t.StudentReport)
.WithRequired();
}
public class StudentReport
{
public int Id { get; set; }
public string RollNumber { get; set; }
public string StudentType { get; set; }
}
See documentation for WithRequired() here

Expand and select a field from a Navigation property which of type collection in Breeze

I have 3 classes in EF as follows
Public class ClasssA
{
public int Id { get; set; }
public int ClassBId { get; set; }
public virtual ICollection<ClassB> ClassB { get; set; }
}
Public class ClassB
{
public int Id { get; set; }
public int ClassCId { get; set; }
public virtual ClassC ClassC { get; set; }
}
Public class ClassC
{
public int Id { get; set; }
public String Value {get; set; }
}
While firing a odata request from browser,it works fine.
http://localhost:8090/odata/ControllerName?$expand=ClassB/ClassC&$select=ClassB/ClassC/Value
But can anybody tell me how can I do in breeze query?
since ClassB navigation property is a of type collection and each of it contains ClassC navigation property.
How can I select a property inside ClassC with this type of navigation?
In simple my question is how to access a property inside, by expanding a Navigation Property of type Collection (in Breeze)?
I do not believe this is supported by BreezeJS. You are welcome to add it to our UserVoice

EF6, DTO, update navigation property

I am using Entity Framework 6 with Generic Repository and DTOs.
I want to create new entities via navigation property.
Here is my model:
public partial class Project
{
public Project()
{
this.ProjectAssets = new List<ProjectAsset>();
}
public int ProjectID { get; set; }
public string Name { get; set; }
public virtual ICollection<ProjectAsset> ProjectAssets { get; set; }
}
public partial class Asset
{
public Asset()
{
this.Revisions = new List<Revision>();
}
public int AssetID { get; set; }
public string Name { get; set; }
public short Type { get; set; }
public virtual ICollection<Revision> Revisions { get; set; }
}
public partial class ProjectAsset
{
public int MappingID { get; set; }
public int ProjectID { get; set; }
public int AssetID { get; set; }
public virtual Asset Asset { get; set; }
}
I have already created Project. And if i am creating new Asset, then create Project Asset with AssetID from just created Asset, it's OK, but i have to re-fetch Project from DB.
I want to do it in one transaction, like that:
Project.ProjectAssets.Add(new ProjectAsset(new Asset((short)type, fileName)));
ServiceLocator.Default.ResolveType<IPipeLine>().Update(Project);
public void Update<TEntity>(TEntity entity) where TEntity : class
{
var fqen = GetEntityName<TEntity>();
object originalItem;
var key = ((IObjectContextAdapter)DbContext).ObjectContext.CreateEntityKey(fqen, entity);
if (((IObjectContextAdapter)DbContext).ObjectContext.TryGetObjectByKey(key, out originalItem))
((IObjectContextAdapter)DbContext).ObjectContext.ApplyCurrentValues(key.EntitySetName, entity);
//DbContext.Entry(entity).State = EntityState.Modified;
}
But after SaveChanges there is no record in DB, and MappingID still 0.
I thought that ApplyCurrentValues must work with Navigation Properties.
Is there any good way to solve that problem?
EDIT:
I accessing DAL throughBusiness Entities with contain the same properties, but they also implement INotifyPropertyChanged and other WPF stuff. So i think i can subscribe to CollectionChanged event and manualy create/delete entities from navigation property. And in property setters i can call update, but i think it can strongly decrease perfomance.

Collection Navigation Properties using Inheritance (Table Per Type )

Using Entity Framework: Code First, I am trying to define an collection navigation property using a navigation property of a base class.
Object structure:
public class Content
{
public int ID { get; set; }
public ModerationStatuses ModerationStatus { get; set; }
public ContentItemTypes ContentType { get; set; }
public virtual User Author { get; set; }
}
public class Image : Content
{
public Image()
: base()
{
ContentType = ContentItemTypes.Image;
}
public string FileName { get; set; }
public string Location { get; set; }
public int DisplayOrder { get; set; }
public long FileSize { get; set; }
}
public class User
{
public int UserID { get; set; }
public string Username { get; set; }
public virtual ICollection<Image> Images { get; set; }
}
Context OnModelCreating:
modelBuilder.Entity<Content>()
.Map(i => i.ToTable("Content"));
modelBuilder.Entity<Image>()
.Map(i => i.ToTable("Images"));
When the database is generated, it creates a User_UserID foreign key constraint into the Images table instead of using Author_UserID in the Content table.
How can I get it to recognize Content..Author_UserID field as the foreign key for the ICollection<Image> navigation property?
It's not possible. You either need to move the Author property from the Content into the Image class or make the collection in User of type ICollection<Content> Contents. A navigation property must always be declared in the entity class the inverse navigation property is refering to. It cannot be inherited from a base entity.
Actually it is possible... You just need to add a navigation property to your Content class so that you then have a reference to map to.
Here's the thorough walkthrough.