Entity Framework One-To-One relationship - entity-framework-4.1

I have the following view
vw_Resources
-> ResourceId
-> Name
-> ReportsTo (maps to ResourceId)
and the class
public class Resource
{
public int ResourceId{get;set;}
public string Name{get;set;}
public Resource ReportsTo{get;set;}
}
and the DbContext
public class MyContext
{
public DbSet<Resource> Resources { get; set; }
}
How do I map the ReportsTo so that the DbContext returns a Resource object. I have to do this in the following method in the DbContext class using the ModelBinder
protected override void OnModelCreating(DbModelBuilder modelBuilder)

The following mapping worked
modelBuilder.Entity<Resource>().HasRequired(r => r.ReportsTo).WithMany().Map(r => r.MapKey("ReportsTo"));

modify your entity like this
public class Resource
{
public int ResourceId{get;set;}
public string Name{get;set;}
public int? ReportsToId{get;set;}
public Resource ReportsTo{get;set;}
public ICollection<Resource> Reporters{get;set;}
}
then the mapping as
modelBuilder.Entity<Resource>().HasOptional(r => r.ReportsTo)
.WithMany(r => r.Reporters)
.HasForeignKey(r => r.ReportsToId);

You cannot have self referencing one-to-one relation because ResourceId is your PK. If you make it also FK you would say that dependent Resource is related to the principal resource with the same ResourceId but it cannot be because ResourceId must be unique. Self referencing one-to-one relation is not possible in EF at all because EF doesn't support unique non primary keys.

Related

How to create a dependency graph using composite keys in EF Core

Trying to store a composite key table which is keyed for both fields to the table it defines dependencies for.
Example case
Import files: 1..10
Dependencies 1: 2,3; 2: 4,5; 4:10
Intent is to use this key-only table for code to do code first strongly typed definitions while also being light weight, and it seemed like the most straight forward way to do it before running into problems.
Current code:
public class ImportFileDependency
{
[Key]
[ForeignKey("ImportFile")]
public int ImportFileId { get; set; }
[ForeignKey("Id")]
public ImportFile ImportFile {get; set;}
[Key]
[ForeignKey("ImportFile")]
public int ImportFileDependencyId { get; set; }
[ForeignKey("Id")]
public ICollection<ImportFile> ImportFileDependencies { get; set; }
}
public class ImportFile
{
[Key]
public int Id {get; set;}
public string Name { get; set; }
public string WorkbookTab { get; set; }
public string File { get; set; }
public ICollection<ImportFileDependency> Dependencies { get; set; }
}
...
modelBuilder
.Entity<ImportFileDependency>(e =>{
e.HasKey(ifd => new { ifd.ImportFileId, ifd.ImportFileDependencyId });
e.HasOne(ifd => ifd.ImportFile)
.WithMany(i => i.Dependencies);
});
modelBuilder
.Entity<ImportFile>()
.HasMany(i => i.Dependencies)
.WithOne()
.HasForeignKey(z => z.ImportFileId);
...
After multiple revisions of following the responses of the add-migration exception response, currently on:
There are multiple properties pointing to navigation 'ImportFile' in entity type 'ImportFileDependency'. To define composite foreign key using data annotations, use ForeignKeyAttribute on navigation.
which did not update from the most recent iteration.
I seem to have recursed into a deadend so looking for guidance
Given the time you've asked it, you probably found the answer yourself or gave up on it, but if someone else struggles with this error, this solved my issue: Entity Framework Code First - two Foreign Keys from same table
You have to define the relationship using fluent API.

Entity Framework 4.1 Code first mapping to tables that have their primary key as the foreign key column

I have an existing database that I'm using Entity Framework Code First to map. The naming convention for columns is odd, so I decided I'd map entity properties manually, and up until now this has been fine.
The schema for the database is fairly strange to me and is definitely not how I would've done it. Unfortunately, I'm stuck with it for the time being.
Basically there is a single primary key (AccountNumber) shared by a number of tables creating a bunch of one-to-one relationships. However, the primary key is also the foreign key column. Here is what my entities look like (with a whole bunch of properties removed for simplicity). I've only included two entities to make it easy.:
public class Customer
{
public int AccountNumber { get; set; }
public String PhoneNumber { get; set; }
...
public virtual Address Address { get; set; }
}
public class Address
{
public int AccountNumber { get; set; }
public String Name { get; set; }
public String Address1 { get; set; }
public String City { get; set; }
...
public virtual Customer Customer { get; set; }
}
The two entities share the same primary key. I've created configuration classes to do the mapping like this:
public class CustomerConfiguration : EntityTypeConfiguration<Customer>
{
public CustomerConfiguration()
: base()
{
HasKey(p => p.AccountNumber);
Property(p => p.AccountNumber).
HasColumnName("cm_l_acct").
IsRequired();
Property(p => p.PhoneNumber).
HasColumnName("cm_s_phonenumber");
HasRequired(x => x.Address).
WithRequiredPrincipal(x => x.Customer).
Map(x => x.MapKey("am_l_acct"));
}
}
public class AddressConfiguration : EntityTypeConfiguration<Address>
{
public AddressConfiguration()
: base()
{
HasKey(p => p.AccountNumber);
Property(p => p.AccountNumber).
HasColumnName("am_l_acct").
IsRequired();
...
}
}
The foreign key mapping is only done on one side. This appears like it would work if not for the fact that the foreign key column is also the primary key of the table. When I try to run a query, I get the error:
(256,6): error 0019: Each property name in a type must be unique. Property name 'am_l_acct' was already defined.
Unfortunately, I can't pull the mapping for the AccountNumber property off of the Address entity because it is the primary key.
Is there a way I can accomplish this mapping, or is it impossible?
Removet this Map(x => x.MapKey("am_l_acct")) from your Customer mapping. This mapping is only used if you map want to define FK column in database and you don't have FK property in the class but you have it - it is primary key in the Address entity. If you try to map FK that way EF thinks that you are trying to create to columns with the same name.

EF CodeFirst self-referential Many-to-Many...on abstract or derived classes

I'm trying to model a self-referencing many to many in EF CodeFirst with a polymorphic table structure. I'm using the October 2011 CTP which supports navigation properties on derived types (which works well in other tests I've done).
The problem:
When I set up this particular many to many relationship in the base (abstract) table's mapping and try to get related records, I get a SQL query with hundreds of K of unions and joins...just the time taken to generate the SQL statement is 30 seconds, compared to bare milliseconds to execute it. However, it does return appropriate results. When I change the many to many to exist between two derived objects, the query produced is perfect...but I can't map the same relating M2M table again for other derived objects without being informed that the joining table has "already been mapped".
Specifics:
An existing database structure has a base table--Party--which is joined 1...1 or 0 with Customer, Vendor, User, and Department (each a type of Party).
Parties are related to each other via an existing join table PartyRelationship (ID, InternalPartyID, ExternalPartyID). By convention, InternalPartyID contains a User's PartyID and ExternalPartyID contains the PartyID of the Customer, Vendor, or Department with which they are associated.
Trying to use EF CodeFirst in a new project (WCF DataServices), I have created the Party class as:
public abstract class Party
{
public Party()
{
this.Addresses = new List<Address>();
this.PhoneNumbers = new List<PhoneNumber>();
this.InternalRelatedParties = new List<Party>();
this.ExternalRelatedParties = new List<Party>();
}
public int PartyID { get; set; }
public short Active { get; set; }
//other fields common to Parties
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
public virtual ICollection<Party> InternalRelatedParties { get; set; }
public virtual ICollection<Party> ExternalRelatedParties { get; set; }
}
Then, using TPT inheritance, Customer, Vendor, Department and User are similar to:
public class Customer : Party
{
public string TermsCode { get; set; }
public string DefaultFundsCode { get; set; }
//etc
}
public class User : Party
{
public string EmployeeNumber { get; set; }
public string LoginName { get; set; }
//etc
}
The joining table:
public class PartyRelationship
{
public int PartyRelationshipID { get; set; }
public int InternalPartyID { get; set; }
public int ExternalPartyID { get; set; }
//certain other fields specific to the relationship
}
Mappings:
public class PartyMap : EntityTypeConfiguration<Party>
{
public PartyMap()
{
// Primary Key
this.HasKey(t => t.PartyID);
// Properties
this.ToTable("Party");
this.Property(t => t.PartyID).HasColumnName("PartyID");
this.Property(t => t.Active).HasColumnName("Active");
//etc
// Relationships
this.HasMany(p => p.InternalRelatedParties)
.WithMany(rp => rp.ExternalRelatedParties)
.Map(p => p.ToTable("PartyRelationship")
.MapLeftKey("ExternalPartyID")
.MapRightKey("InternalPartyID"));
}
}
public class PartyRelationshipMap : EntityTypeConfiguration<PartyRelationship>
{
public PartyRelationshipMap()
{
// Primary Key
this.HasKey(t => t.PartyRelationshipID);
// Properties
// Table & Column Mappings
//this.ToTable("PartyRelationship"); // Commented out to prevent double-mapping
this.Property(t => t.PartyRelationshipID).HasColumnName("PartyRelationshipID");
this.Property(t => t.InternalPartyID).HasColumnName("InternalPartyID");
this.Property(t => t.ExternalPartyID).HasColumnName("ExternalPartyID");
this.Property(t => t.CreateTime).HasColumnName("CreateTime");
this.Property(t => t.CreateByID).HasColumnName("CreateByID");
this.Property(t => t.ChangeTime).HasColumnName("ChangeTime");
this.Property(t => t.ChangeByID).HasColumnName("ChangeByID");
}
}
Context:
public class MyDBContext : DbContext
{
public MyDBContext()
: base("name=MyDBName")
{
Database.SetInitializer<MyDBContext>(null);
this.Configuration.ProxyCreationEnabled = false;
}
public DbSet<Party> Parties { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
modelBuilder.Configurations.Add(new PartyMap());
modelBuilder.Configurations.Add(new PartyRelationshipMap());
}
}
A URL such as http://localhost:29004/Services/MyDataService.svc/Parties(142173)/SAData.Customer/InternalRelatedParties eventually returns correct oData but takes 30 seconds to produce an enormous SQL statement (189K) that executes in 600 ms.
I've also tried mapping the PartyRelationship table with a bidirectional one to many (both to Party as the "one" table), but with a similar outcome.
Do I need separate join tables for Customer-User, Vendor-User, and Department-User? Should I look at vertical table splitting or database views that separates PartyRelationship into separate logical entities (so I can remap the same table)? Is there another way the EF model should be configured in this scenario?

entity framework fluent api - create forgein key based on naming convention

I start learn EF Fluent API.
I have 2 simple POCO classes.
public class Customer
{
public int CustomerId{ get; set;}
public string Name{ get; set;}
}
public class Project
{
public int ProjectId { get; set; }
public int CustomerId { get; set; }
public string Name { get; set; }
public Customer Customer { get; set; }
}
context class
public class MyCtx:DbContext
{
public DbSet<Project> Projects { get; set; }
public DbSet<Customer> Authors { get; set; }
public MyCtx(string connString):base(connString)
{}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//PK
modelBuilder.Entity<Project>()
.HasKey(p => p.ProjectId)
.Property(p => p.ProjectId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName("PROJECT_ID")
.IsRequired();
modelBuilder.Entity<Project>()
.Property(c => c.Name)
.HasColumnName("NAME")
.IsRequired();
//--------------------------------------------------------------------
//PK
modelBuilder.Entity<Customer>()
.HasKey(c => c.CustomerId)
.Property(c => c.CustomerId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName("CUSTOMER_ID")
.IsRequired();
modelBuilder.Entity<Customer>()
.Property(c => c.Name)
.HasColumnName("NAME")
.IsRequired();
base.OnModelCreating(modelBuilder);
}
}
I defined that CustomerId will be primary key in table customer and ProjectId will be primary key in project table.
I am little surprised with this behavarior. CustomerId in project table is automatically foreign key.
This behavior is based on naming convention? Or how it works?
Yes, it is based on a naming convention, in this case specifically the NavigationPropertyNameForeignKeyDiscoveryConvention:
Convention to discover foreign key properties whose names are a
combination of the dependent navigation property name (Customer in your case) and the
principal type primary key property name(s) (CustomerId in your case).
Or it is the PrimaryKeyNameForeignKeyDiscoveryConvention:
Convention to discover foreign key properties whose names match the
principal type primary key property name(s).
I am not sure which one.
If you don't like one of those conventions you can remove them with the model builder:
modelBuilder.Conventions
.Remove<NavigationPropertyNameForeignKeyDiscoveryConvention>();
// etc.
A complete list of all conventions is here.

One to one relation with EF 4.1 code first

My question is similar to this one :
--> Many to one configuration using EF 4.1 code first
There are some fluent API solutions on google, with overriding "OnModelCreating" method and manually setting the foreign key options. But i would prefer a solution with data annotations if it is possible. Because I'd like to use inverse properties while coding. Such as TypeA object has got a TypeB object. So TypeB object should have a ParentTypeA property. Example :
public class User : IUser
{
[Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[RegularExpression(#"[A-Za-z0-9_\-\.]{2,32}"), MinLength(2), MaxLength(32)]
[Required(AllowEmptyStrings = false)]
public string UserName { get; set; }
// other props ....
// ....
public virtual UserGallery Gallery { get; set; }
}
public class UserGallery : IUserGallery
{
[Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserGalleryId { get; set; }
// other props ....
// ....
public virtual User ParentUser { get; set; }
}
A conventions way to do this in Code First is to use the UserID as the Primary Key of the UserGallery object. This is fine if its a true one to one relationship.
public class UserGallery : IUserGallery
{
[Key]
public int UserId {get;set;}
public User User {get;set;}
etc...
}
This has worked fine for me in the past.