I have an entity type MyEntity that has a primary key string MyEntityCode
I want to make a second entity MyEntityInfo that are extended properties that some MyEntity's are logically associated.
That makes the relationship between these entities one-to-one, with one end optional -- MyEntity logically optionally has a MyEntityInfo, without a navigation property, and MyEntityInfo is required to have a single MyEntity (with a navigation property).
I want to encode this in SQL as MyEntityInfo having a primary key BaseEntityCode that's also a foreign key to MyEntity's MyEntityCode.
How do I configure this encoding in EF6 fluent configuration API.
Sample code
public class MyEntity {
public string MyEntityCode {get; set;}
public int SomeProperty {get; set;}
}
public class MyEntityInfo {
public MyEntity BaseEntity {get; set;}
public string BaseEntityCode {get; set;}
public int OtherInfo {get; set;}
}
public MyEntityConfiguration : EntityConfiguration<MyEntity> {
public MyEntityConfiguration(){
HasKey(e => e.MyEntityCode);
}
}
I thought I could configure MyEntityInfo as
public MyEntityInfoConfiguration : EntityConfiguration<MyEntityInfo> {
public MyEntityInfoConfiguration(){
HasKey(e => e.BaseEntityCode);
HasRequired(e => e.BaseEntity).WithOptional().WithForeignKey(e => BaseEntityCode);
}
}
but WithOptional() doesn't allow chaining to WithForeignKey
Doing the same but with WithMany() so that a foreign key is possible, the multiplicity constraint of one is violated:
Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.
I thought I could configure MyEntityInfo as
public class MyEntityInfoConfiguration : EntityTypeConfiguration<MyEntityInfo>
{
public MyEntityInfoConfiguration(){
HasKey(e => e.BaseEntityCode);
HasRequired(e => e.BaseEntity).WithOptional().WithForeignKey(e => BaseEntityCode);
}
}
Well, almost, just remove the WithForeignKey call!
public class MyEntityInfoConfiguration : EntityTypeConfiguration<MyEntityInfo>
{
public MyEntityInfoConfiguration()
{
HasKey(e => e.BaseEntityCode);
HasRequired(e => e.BaseEntity).WithOptional();
}
}
Entity Framework 6 has only one implementation of 1:1 associations: the primary key of the dependent entity (here: MyEntityInfo) is the foreign key to the principal entity (here: MyEntity).
There is no WithForeignKey method because with your proposed mapping (without WithForeignKey) EF knows all it needs to know now for the only implementation of 1:1 it has in store.
The produced database model shows the primary key/foreign key dual role of BaseEntityCode:
CREATE TABLE [dbo].[MyEntities] (
[MyEntityCode] [nvarchar](128) NOT NULL,
[SomeProperty] [int] NOT NULL,
CONSTRAINT [PK_dbo.MyEntities] PRIMARY KEY ([MyEntityCode])
)
CREATE TABLE [dbo].[MyEntityInfoes] (
[BaseEntityCode] [nvarchar](128) NOT NULL,
[OtherInfo] [int] NOT NULL,
CONSTRAINT [PK_dbo.MyEntityInfoes] PRIMARY KEY ([BaseEntityCode])
)
...
ALTER TABLE [dbo].[MyEntityInfoes]
ADD CONSTRAINT [FK_dbo.MyEntityInfoes_dbo.MyEntities_BaseEntityCode]
FOREIGN KEY ([BaseEntityCode]) REFERENCES [dbo].[MyEntities] ([MyEntityCode])
Related
EF Core 3.0... I can't find a precise answer for this completely normal mapping.
Principal to Dependent with no back pointer to Principal, 1:0 relationship, a Type Object / Lookup table set up. The problem is that the Object Key Name "RunId" is different than the EFCore generated key name "ServiceRunId"
How can I use Fluent API to replace the [ForeignKey("aServiceRun")] annotation?
This is my current Fluent set up, but I don't know where to put the ForeignKey mapping.
aBuilder.Entity<ServiceRun>().HasKey(new string[] { "RunId "});
aBuilder.Entity<Service>().HasOne(s => s.aServiceRun);
Class Service {
public int ServiceId {get; set;}
[ForeignKey("aServiceRun")]
public int RunId { get; set; }
public virtual ServiceRun aServiceRun { get; set; }
}
Class ServiceRun {
public int RunId { get; set; }
public string description {get ;set; }
}
Tables:
Service {
ServiceId int
RunId int
}
SerivceRun {
RunId int
Description string
}
How can I use Fluent API to replace the [ForeignKey("aServiceRun")] annotation?
You are seeking for HasForeignKey fluent API. But in order to get access to it (and other relationship configuration APIs), you need to define the relationship by using Has{One|Many} followed by With{One|Many}. For one-to-one relationships you also need to provide the generic type argument to HasForeignKey:
When configuring the relationship with the Fluent API, you use the HasOne and WithOne methods.
When configuring the foreign key you need to specify the dependent entity type - notice the generic parameter provided to HasForeignKey in the listing below. In a one-to-many relationship it is clear that the entity with the reference navigation is the dependent and the one with the collection is the principal. But this is not so in a one-to-one relationship - hence the need to explicitly define it.
Note that the entity containing the FK is always the dependent, so with your model the ServiceRun is the principal, Service is the dependent, and the fluent configuration is a follows:
modelBuilder.Entity<Service>()
.HasOne(s => s.aServiceRun) // navigation property
.WithOne() // no navigation property
.HasForeignKey<Service>(s => s.RunId); // foreign key
I found my answer to the above problem - I had a back-pointing list on my ServiceRun object that was not configured or ignored. I decided to leave this here as another example. Perhaps it will provide some worth to someone.
This is a 1:0 from Service to ServiceRunType where table names and property/field names don't match perfectly.
Tables
ServiceRun { //Does not match object name
int Id,
string Desc
}
Service {
int Id,
int RunId //Does not match object
}
Objects
Class ServiceRunType{ //Does not match table name
public int Id {get; set;}
public String Desc {get; set;}
}
Class Service{
public int Id {get; set;}
public int RunTypeId {get; set;} //Does not match table
public virtual ServiceRunType aServiceRunType { get; set; }
}
Fluent Code
modelBuilder.Entity<ServiceRunType>()
.ToTable("ServiceRun", schema: "load")
.HasKey(new string[] { "Id" });
modelBuilder.Entity<Service>()
.ToTable("Service", schema: "load") //Had to specify schema
.HasKey(new string[] { "Id" });
modelBuilder.Entity<Service>()
.Property("RunTypeId")
.HasColumnName("RunId");
modelBuilder.Entity<Service>()
.HasOne(s => s.aServiceRunType)
.WithOne()
.HasForeignKey<Service>(s => s.RunTypeId);
I have the following classes generated from an edmx model:
public partial class A
{
public int Id { get; set; }
public virtual B B { get; set; }
}
public partial class B
{
public int Id { get; set; }
public virtual A A { get; set; }
}
The existing db doesn't use the EF default which expects A.Id to be the primary key of table B:
CREATE TABLE [dbo].[B] (
[Id] INT IDENTITY (1, 1) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
CREATE TABLE [dbo].[A] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[BId] INT NULL,
CONSTRAINT [fk] FOREIGN KEY ([BId]) REFERENCES [dbo].[B] ([Id])
);
With an edmx model, I can explicitly configure the multiplicity of each end, but I haven't found how to get the equivalent model using the fluent-api. When I do something like the following and generate a new db, the foreign key gets placed in table A instead of table B.
modelBuilder.Entity<A>().HasOptional(a => a.B).WithRequired(b => b.A);
I'm guessing I need to use a convention, but so far I've been unable to get the desired output.
UPDATE:
The closest solution I've found so far is to use the following which generates the correct SQL in the db:
modelBuilder.Entity<A>()
.HasOptional(a => a.B)
.WithOptionalDependent(b => b.A)
.Map(c => c.MapKey("BId"));
However, it's conceptually modeled as a 0..1:0..1 relationship and I haven't found how to set a CASCADE delete rule that deletes B when A is deleted.
I wasn't able to find a direct solution, but using the following code seems to meet my requirements of preserving the existing schema and creating a conceptual model that has the same multiplicities & delete behaviors as my original edmx model.
I'd still be interested in any solutions that don't require updating the conceptual model during the post-processing IStoreModelConvention.
{
var overridesConvention = new OverrideAssociationsConvention();
modelBuilder.Conventions.Add(overridesConvention);
modelBuilder.Conventions.Add(new OverrideMultiplictyConvention(overridesConvention));
}
private class OverrideAssociationsConvention : IConceptualModelConvention<AssociationType>
{
...
public List<AssociationEndMember> MultiplicityOverrides { get; } = new List<AssociationEndMember>();
public void Apply(AssociationType item, DbModel model)
{
if (multiplicityOverrides.Contains(item.Name))
{
// Defer actually updating the multiplicity until the store model is generated
// so that foreign keys are placed in the desired tables.
MultiplicityOverrides.Add(item.AssociationEndMembers.Last());
}
if (cascadeOverrides.Contains(item.Name))
{
item.AssociationEndMembers.Last().DeleteBehavior = OperationAction.Cascade;
}
}
}
private class OverrideMultiplictyConvention : IStoreModelConvention<EdmModel>
{
private readonly OverrideAssociationsConvention overrides;
public OverrideMultiplictyConvention(OverrideAssociationsConvention overrides)
{
this.overrides = overrides;
}
public void Apply(EdmModel item, DbModel model)
{
overrides.MultiplicityOverrides.ForEach(o => o.RelationshipMultiplicity = RelationshipMultiplicity.One);
}
}
I've posted my problem on codeplex http://entityframework.codeplex.com/workitem/2087.
There are also some questions posted here but they are not successfully answered.
See
Mapping TPT in EF Code First 4.1 w/ Different Primary Keys
Entity Framework 4 - TPT Inheritance in Features CTP5 (code first): rename foreign key column on inherited table
How can I use TPT inheritance models when primary keys have different names?
Is it now possible to have different column names for the primary keys when using TPT?
May be with 6.1.0
In TPT you're essentially do not want to declare the key in the subclasses, you'd miss the point otherwise.
If you must have a different Id name, just make proxy properties in the subclasses mapping to the base Id one.
public class BaseEntity
{
public int Id { get; set; }
}
public abstract class SubEntity : BaseEntity
{
public BaseId
{
get => Id;
set => Id = value;
}
}
Consider marking the sub fields as NotMapped, which in case you shouldn't include them in your LINQ queries.
With EF 6.4 I was able to use the ColumnAttribute to rename the Primary Key column in the dependent class
[Table("Person")]
public class Person
{
[Key]
public virtual int PersonId { get; set; }
// Person atributes...
}
[Table("Employee")]
public class Employee : Person
{
[Column("EmployeeId")] // <- Name of the primary Key column in the Database
public override int PersonId { get; set }
// Employee Attributes
}
Look at this code snip. Its work correct for me:
public partial class Person
{
// Any other PK name can thrown an exception
public int ID { get; set; }
}
public partial class Employee : Person
{
// Hide base class ID
private new int ID { get; set }
// Define derived class ID (that wrapped inherited ID)
[NotMapped]
public int EmployeeID
{
get { return base.PersonID; }
set { base.PersonID = value; }
}
}
Now, we must rename the inherited ID (with fluent API) for database table:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>()
.Property(e => e.ID)
.HasColumnName("EmployeeID");
}
Table EMPLOYEE has MST_SQ (master-sequence) as both it's primary key, and as an FK to the primary key of table MASTER, which is also named MST_SQ. This table is used to join several other tables as well so that they all have the same PK. That is as far as my understanding goes.
I need to defined a 1 to 1 relationship in my model between class Employee and class Master, but I simply cannot find a way to do this. It seems only relationships with multiplicty allow an FK field to be speficied, and those that look like for 1 to 1, e.g. has optional(...)..WithRequiredPrincipal(....) has no FK space.
I could do some manual coding to link EMPLOYEE and MASTER when the are loaded, but how could I tell they were loaded. Is there any event that signals a POCO being populated from the DB? Or, the real question, how do I define this relationship in code?
From Relationships and Navigation Properties :
When working with 1-to-1 or 1-to-0..1 relationships, there is no
separate foreign key column, the primary key property acts as the
foreign key
From Configuring a Required-to-Optional Relationship (One-to–Zero-or-One) :
because the name of the property does not follow the convention the
HasKey method is used to configure the primary key
public class Master
{
public int MST_SQ { get; set; }
public virtual Employee Employee { get; set; }
}
public class Employee
{
public int MST_SQ { get; set; }
public virtual Master Master { get; set; }
}
The Employee has the MST_SQ property that is a primary key and a foreign key:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Master>().HasKey(m => m.MST_SQ);
modelBuilder.Entity<Employee>().HasKey(e => e.MST_SQ);
modelBuilder.Entity<Employee>()
.HasRequired(e => e.Master) //Employee is the Dependent and gets the FK
.WithOptional(m => m.Employee); //Master is the Principal
}
Generated migration code:
CreateTable(
"dbo.Employees",
c => new
{
MST_SQ = c.Int(nullable: false),
})
.PrimaryKey(t => t.MST_SQ)
.ForeignKey("dbo.Masters", t => t.MST_SQ)
.Index(t => t.MST_SQ);
CreateTable(
"dbo.Masters",
c => new
{
MST_SQ = c.Int(nullable: false, identity: true),
})
.PrimaryKey(t => t.MST_SQ);
So you don't need the "FK space" because EF makes it the foreign key without you having to specify it
I have two classes:
public class Account
{
public int Id {get;set;}
public PayoffData PayoffData {get;set;}
}
public class PayoffData
{
public int Id {get;set;}
//some other fields
}
Now, i want class PayoffData to have the same id that class Account has, and it needs to be one-to-one relationship (account can (or can not) have one payoffdata).
I've tried to do something with modelbuilder, but as far i can see that to set up a foreignkey in PayoffData class, i need to set a .HasMany relationship, which i don't want. How can i solve my problem using modelbuilder? (I don't want to use data annotations approach)
modelBuilder.Entity<Account>()
.HasOptional(a => a.PayoffData)
.WithRequired();
Both tables will have a primary key column called Id and the primary key Id in the PayoffData table will be the foreign key to the Account table at the same time.