I have a DB-First approach in the project, so i have to do some manual EF mappings. Is there any way for EF Value Conversion to do a conversion for multiple columns into one record?
All the examples in the documentation are for one property only.
https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions?tabs=data-annotations
Items Table:
Id
ITM_Price
ITM_Currency
ITM_Name
1
420
USD
Item name
Example:
public class Item
{
public long Id { get; set; }
public string Name { get; set; }
public Price Price { get; set; }
}
public record Price(decimal Value, string Currency);
The current EntityTypeBuilder is for the Item.
builder
.Property(e => e.Name)
.HasColumnName("ITM_Name");
builder
.Property(e => e.Price)
.HasColumnName("ITM_Price")
.HasColumnType("decimal(25, 6)")
.HasConversion(
v => v,
v => new Price(v, ???)); // Can't set the currency via constructor / property initialization;
I'd like to keep the configuration in the config for Item mapping two columns into this record but if it's not possible do I require to create a seperate config for Price?
Found a solution for that.
Instead of using conversion, the Owned Entity Types are a good match to solve that issue:
https://learn.microsoft.com/en-us/ef/core/modeling/owned-entities#mapping-owned-types-with-table-splitting
Related
I have an entity with an ExternalSystemName value object and a Deployment parent type which is another entity. The important part of the model looks like this :
public sealed class ExternalSystem : Entity
{
public ExternalSystemName Name { get; private set; }
public Deployment Deployment { get; private set; }
}
The uniqueness of this entity is determined by a combination of the deployment ID (stored in the deployment entity class) and the name (which is the value of the ExternalSystemName value object). In other words, a deployment cannot have 2 external systems with the same name.
I am facing an issue when trying to setup this combined unique index with an IEntityTypeConfiguration implementation :
internal sealed class ExternalSystemsConfiguration :
IEntityTypeConfiguration<ExternalSystem>
{
public void Configure(EntityTypeBuilder<ExternalSystem> builder)
{
builder.ToTable("TblExternalSystems");
builder.OwnsOne(e => e.Name, navigationBuilder =>
{
navigationBuilder.Property(e => e.Value)
.HasColumnName("Name");
});
builder.HasIndex(e => new { e.Name, e.Deployment }).IsUnique();
}
}
I am getting this exception when running my API :
System.InvalidOperationException: ''Name' cannot be used as a property on entity type 'ExternalSystem' because it is configured as a navigation.'
I tried pointing the index to e.Name.Value instead and I am getting this error :
System.ArgumentException: 'The expression 'e => new <>f__AnonymousType0`2(Value = e.Name.Value, Deployment = e.Deployment)' is not a valid member access expression. The expression should represent a simple property or field access: 't => t.MyProperty'. When specifying multiple properties or fields, use an anonymous type: 't => new { t.MyProperty, t.MyField }'. (Parameter 'memberAccessExpression')'
I also tried a unique index on just one of these properties and I get the navigation error regardless. I fear I know the answer already but does this mean EF Core only supports indexes on columns that are not a non-entity, non-valueObject type? Does that mean my model needs to have a Guid property representing the Deployment ID instead of having the Deployment itself?
UPDATE
I learned that EF Core can deal with reference / primitive pairs just fine. With that in mind, my ExternalSystem entity can now have BOTH these properties :
public Deployment Deployment { get; private set; }
public Guid DeploymentId { get; private set; }
That Guid property is not part of the constructor and because they ultimately get the same column name everything works fine. I can now just add this to my configuration for this entity and the index is created properly :
builder.HasIndex(e => new { e.DeploymentId}).IsUnique();
My issue is now with the value object. Using the same approach, I suppose I could do something like this ?
public ExternalSystemName NameV { get; private set; }
public string Name { get; private set; }
I have to rename the value object property since they obviously can't share the same name. This is not something I had to do with the entity type since EF Core knew to add "Id" to the column name in the first place. With this setup, EF Core is duplicating the columns. One has the name "Name" and the other one has "ExternalSystem_Name". Obviously everthing else fails from there since that column doesn't accept null values. Why is this happening?
I fixed this by defining a converter for the ExternalSystemName property :
builder.Property(e => e.Name)
.HasConversion(
e => e.Value,
v => ExternalSystemName.Create(v).Value);
I can now declare my unique index as such :
builder.HasIndex(e => new { e.DeploymentId, e.Name}).IsUnique();
I also upgraded to .NET 7 and EF Core 7 since asking this question so it may have been a factor as well.
You need to manually define the key to be the foreign key using Fluent api.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ExternalSystem>()
.HasOne(g => g.NameV)
.WithOne()
.HasForeignKey<ExternalSystem>(j => j.Name);
}
Put this method inside your DB context class
We have a enum Supplier
But now we need to also have some Domain data on that relation
So in 99.9% in the domain code we doe operations on the enum like product.Supplier == Suppliers.FedEx
But now we also have added product.SupplierInfo.CanAdjustPickupTime where SupplierInfo is a Entity and not just a simple enum type.
I have tried these configs
Property(p => p.Supplier)
.IsRequired()
.HasColumnName("SupplierId");
HasRequired(p => p.SupplierInfo)
.WithMany()
.HasForeignKey(p => p.Supplier); //I have also tried casting to int doing .HasForeignKey(p => (int)p.Supplier)
This will fail with
The ResultType of the specified expression is not compatible with the
required type. The expression ResultType is
'MyApp.Model.Suppliers' but the required type is
'Edm.Int32'. Parameter name: keyValues[0]
Also tried
Property(l => l.Supplier)
.IsRequired()
.HasColumnName("SupplierId");
HasRequired(p => p.SupplierInfo)
.WithMany()
.Map(m => m.MapKey("SupplierId"));
This will offcourse give the good old
One or more validation errors were detected during model generation:
SupplierId: Name: Each property name in a type must be unique.
Property name 'SupplierId' is already defined.
I could offcourse define SupplierId as a Property use that with HasForeignKey But then I need to change to .SuppliedId == (int)Suppliers.FedEx etc. Not really a solution.
I could also add a property enum that uses the SupplierId property as backing field, but this will not work with Expressions since it needs to use real mapped DB properties
Any ideas?
I have classes:
public class Agreement
{
public int Id { get; set; }
public AgreementStateTypeEnum AgreementStateId { get; set; }
}
public class AgreementState
{
public int Id { get; set; }
public string Title { get; set; }
}
context:
public class AgreementContext :DbContext
{
public AgreementContext() : base("SqlConnection") { }
public DbSet<Agreement> Agreements { get; set; }
}
In method OnModelCreating I wrote nothing.
My enum:
public enum AgreementStateTypeEnum : int
{
InReviewing = 1,
Confirmed = 2,
Rejected = 3
}
In database: in table Agreements I have foreign key AgreementStateId - it is link to table AgreementStates.
Everything is working. For example:
var temp = context.Agreements.First(x => x.AgreementStateId == AgreementStateTypeEnum.Confirmed);
I use enum how foreign key.
Finally I found the problem. (I'm using EF6, NET 4.5)
So, if you create a type Enum in your code, you couldn't create a relationship with other property virtual.
//This is wrong, when do you create a foreignkey using a type enum
//Do You should remove that's code on in your class Map.
HasRequired(p => p.SupplierInfo)
.WithMany()
.HasForeignKey(p => p.Supplier); //I have also tried casting to int doing
.HasForeignKey(p => (int)p.Supplier)
If did you created a type enum it means that you don't need for a table return data throught for a join in EF.
So, the correct code it is:
public class MyClass{
public enum myEnumType {
FedEx,
Olther
}
public int id {get;set;}
public myEnumType Supplier {get;set;}
}
//My class Map (using Fluent...)
public class MyClassMap {
HasKey(t => t.Id);
Property(t => t.Id).HasColumnName("Id");
//The type [supplier] should be [int] in database.
Property(t => t.Supplier).HasColumnName("supplier");
//That's all, you don't need write relationship, int this case
//Because, when the data returns, the EF will to do the conversion for you.
}
I hope that's useful
The best way I have found to deal with this scenario is to map Supplier as a regular domain object and create a separate class of known supplier IDs.
public class KnownSupplierIds
{
public const int FedEx = 1;
public const int UPS = 2;
// etc.
}
if (product.Supplier.SupplierId == KnownSupplierIds.Fedex) { ... };
When your code needs to check the supplier, it can compare the IDs; when you need additional info from the domain model you just load the Supplier. The reason I prefer using a class of constants instead of an enum is that the pattern works for string comparisons also and there's no need to cast.
I have an employee table that self references to determine organization structure. I'm having some trouble trying to set this up using Code-First (POCO) fluently.
An employee record has both a "Position" field and a "ReportsTo" field and neither of the columns are the primary key (employee.id).
An employee with a "ReportsTo" value of "08294" , is an employee of a direct report of an employee with "Position" value of "08294".
Can anyone offer up some info on how to set this up using EF code first, fluently...is it possible?
I tried the code below and am getting error:
Employee_Employees_Source_Employee_Employees_Target: : The types of
all properties in the Dependent Role of a referential constraint must
be the same as the corresponding property types in the Principal Role.
The type of property 'ReportsTo' on entity 'Employee' does not match
the type of property 'Id' on entity 'Employee' in the referential
constraint 'Employee_Employees'.
Employee.cs
public class Employee
{
public int Id { get; set; } //pk
public string Position { get; set; } // i.e. 06895
public string ReportsTo{ get; set; } // i.e. 08294
public virtual Employee Supervisor { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
DbContext
modelBuilder.Entity<Employee>()
.HasMany(e => e.Employees)
.WithOptional(e => e.Supervisor)
.HasForeignKey(e => e.ReportsTo);
I think more than anything, I would like to keep the POCO free of EF "stuff" and be able to do something like:
employee.IsSupervisor(); // based on child employee count.
The issue is in the relationship configuration. If you want to configure your one to many relation without using a FK, you could do this:
modelBuilder.Entity<Employee>()
.HasMany(e => e.Employees)
.WithOptional(e => e.Supervisor);
Now if you want to use a FK property, then add this property to your model class:
public class Employee
{
//...
public int SupervisorId { get; set; }
}
And map your relationship this way:
modelBuilder.Entity<Employee>()
.HasMany(e => e.Employees)
.WithOptional(e => e.Supervisor)
.HasForeignKey(e => e.SupervisorId);
To resolve your issue related with ReportTo and Position properties,I think you should handle that logic in your code. If you want to know if an Employee is a supervisor based on the count of Employees property, you could use a NotMapped property:
public class Employee
{
[NotMapped]
public bool IsSupervisor
{
get
{
return Employess.Count>0
}
}
}
You can do the same using Fluent Api:
modelBuilder.Entity<Employee>().Ignore(e => e.IsSupervisor);
PS: Remember initialize Employees in your class'constructor.
The error you get is because it is trying to map a PK of int type to a FK of string type. User int for all of your key fields.
Then, you need to declare your OnModelBuilding like this:
modelBuilder.Entity<Employee>()
.HasOptional(e => e.Supervisor)
.WithMany()
.HasForeignKey(s => s.ReportsTo);
To get something like IsSupervisor() you can take advantage of partial classes. Create another class file which is a public partial class Employee (and modify your original one to be partial), then in your new file you will add a property that does whatever you want, and decorate it with [NotMapped] attribute. Yours will probably look something like public bool IsSupervisor {get { return (Employees == null) ? false : true; } set {} } The new partial class is where you can do whatever you want for the POCO without changing the EF class (make sure you use [NotMapped] though).
Using Entity Framework (Code First), I'm trying to map a conditional/filtered relationship between the following 2 entities:
Building
BuildingId
BuildingName
Area
AreaId
ParentId
AreaName
IsSubArea
A Building can have many Areas
An Area can have many (Sub)Areas
I would like to create the relationship between Building and Area where the areas marked with 'IsSubArea' are filtered out of the relationship. In this context, ParentId would relate to a Building, otherwise, ParentId would be another Area. This would allow me to create a building with many areas, and each area could have many sub-areas, creating a tree style structure.
The closest to a solution I have found relates to 'soft delete' functionality (source):
modelBuilder.Entity<Foo>().Map(m => m.Requires("IsDeleted").HasValue(false));
Converted to fit my example:
modelBuilder.Entity<Area>().Map(m => m.Requires("IsSubArea").HasValue(false));
But as far as I can tell, this has no bearing on the relationship to the Building.
Another solution would be to create a property on the Building which specifies the query definition to use to return related areas (source):
public class Building
{
public int BuildingId {get; set;}
public string BuildingName {get; set;}
public IQueryable<Area> BuildingAreas
{
get
{
return from area in areas
where area.IsSubArea == false
and area.ParentId == BuildingId
select area;
//Assume I have a reference to relevant DbSets
}
}
}
This solution would work but doesn't feel as elegant as a conditional mapping.
Another solution would be to inherit from Area and create the 2 sub-classes:
BuildingArea
AreaId
BuildingId
AreaName
SubArea
AreaId
ParentAreaId
AreaName
Each inherits from Area and sets the 'IsSubArea' field as appropriate.
This solution feels tidier but I do not know how to implement this in Entity Framework.
Is there a way to specify conditional mapping on relationships?
Is there a better way to implement this structure?
Update 1:Found this & this guide on inheritance which seems to match my requirements. However, neither of these tutorials define relationships between derived types. I'll update the question tonight with what I have tried with regards to the Table per Hierarchy (TPH) method.
Update 2:
I'm going to try an describe the Table per Hierarchy (TPH) method I tried to implement based on the tutorial links above. Forgive me if this gets a little complicated (maybe I'm over thinking it).
Models
The building class remains the same as the OP.
I have created an abstract base class defining the Area properties common to each derived type (BuildingArea and SubArea):
public abstract class Area
{
protected Area(bool isSubArea)
{
IsSubArea = isSubArea;
SubAreas = new List<SubArea>();
}
public int AreaId { get; set; }
public int ParentId { get; set; }
public string AreaName { get; set; }
public bool IsSubArea { get; private set; } //note the private set
public virtual ICollection<SubArea> SubAreas { get; set; }
}
I then have 2 derived types which inherit from Area:
public class BuildingArea : Area
{
public BuildingArea () : base(false)
{}
public virtual Building ParentBuilding { get; set; }
}
public class SubArea : Area
{
public SubArea(): base(true)
{}
// This is of type `Area` because parent could be either `BuildingArea` or `SubArea`
public virtual Area Parent { get; set; }
}
I then have the following 2 EntityTypeConfigurations:
public class BuildingAreaMap : EntityTypeConfiguration<BuildingArea>
{
public BuildingAreaMap ()
{
// Primary Key
HasKey(t => t.AreaId);
// Properties
Property(t => t.AreaName)
.IsRequired()
.HasMaxLength(256);
// Table & Column Mappings
ToTable("Areas");
Property(t => t.AreaId).HasColumnName("AreaId");
Property(t => t.ParentId).HasColumnName("ParentId");
Property(t => t.AreaName).HasColumnName("AreaName");
Property(t => t.IsSubArea).HasColumnName("IsSubArea");
// This is the discriminator column
Map(m => m.Requires("IsSubArea").HasValue(false));
HasRequired(a => a.Site).WithMany(s => s.SiteAreas).HasForeignKey(k => k.ParentId);
}
public class SubAreaMap : EntityTypeConfiguration<SubArea>
{
public SubAreaMap()
{
// Primary Key
HasKey(t => t.AreaId);
// Properties
Property(t => t.AreaName)
.IsRequired()
.HasMaxLength(256);
// Table & Column Mappings
ToTable("AssetHealthAreas");
Property(t => t.AreaId).HasColumnName("AreaId");
Property(t => t.ParentId).HasColumnName("ParentId");
Property(t => t.AreaName).HasColumnName("AreaName");
Property(t => t.IsSubArea).HasColumnName("IsSubArea");
// This is the discriminator column
Map(m => m.Requires("IsSubArea").HasValue(true));
HasRequired(a => a.Parent).WithMany(s => s.SubAreas).HasForeignKey(k => k.ParentId);
}
}
This code builds successfully, but I do get the following runtime error:
Map was called more than once for type 'SiteArea' and at least one of the calls didn't specify the target table name.
But I am specifying the target table name (once in each EntityTypeConfiguration class).
So I removed the EntityTypeConfiguration for SubArea but I get the same error.
One of the tutorials pulls the mapping out of the EntityTypeConfiguration class and puts it in the OnModelCreating handler as follows:
modelBuilder.Entity<Area>()
.Map<BuildingArea>(m => m.Requires("IsSubArea").HasValue(false))
.Map<SubArea>(m => m.Requires("IsSubArea").HasValue(true));
This also gives me the same error.
If I remove relationships from the equation, I get a different error regarding the ParentId property:
The foreign key component 'ParentId' is not a declared property on type 'SiteArea'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.
Update 3
An image of the model I'm trying to create...
Update 4
I'm going to try and simplify my model to match the following.
If the solution below works, I will need to have a little more business logic to navigate the tree but it should be manageable.
For the errors when creating the TPH classes:
I think this is because you are not supposed to have the discriminator column as a property in your classes.
Remove the property IsSubArea from your base class.
When you create new instance, EF should automatically detect the type and fill the IsSubArea accordingly.
I have an aggregate class that will contain a collection of another class, but that class will only ever exist in a collection on that aggregate, so I have no need for an ID on it in my code, or a need for a reference to the aggregate. For example:
public class SalesListing
{
public Guid Id { get; set; }
public ICollection<LocalizedDescription> Descriptions { get; set; }
}
public class LocalizedDescription
{
public string CultureCode { get; set; }
public string Title { get; set; }
}
I'd like to just declare the key for the LocalizedDescription class as a combination of SalesListingId and CultureCode without creating a SalesListingId property or reference back to SalesListing. Any way to do this with EF 5.0 ?
For example, here's an example how I'd imagine such an API would look like if it exists:
modelBuilder.Entity<LocalizedDescription>().BelongsTo<SalesListing>(s => s.Description)
.WithKey((s, ld) => new { s.Id, ld.CultureCode })
No, you will still need to specify an ID
You can define composite keys using the following syntax:
modelBuilder.Entity<LocDesc>().HasKey(ld => new { ld.CultureCode, ld.Title });
That said, I think you will regret not defining an integer primary key. If you ever need to export the contents (such as for an external review or translation), matching records by ID will be faster and less error prone than matching by title. Having the ID doesn't hurt and is easier to include now than to retrofit later.
I think you can specify the FK mapping implicitly by not supplying a specific property for the relation in the mapping definition:
modelBuilder.Entity<SalesListing>().HasMany( e => e.Descriptions ).WithRequired();
Should you need to specify the foreign key property, introduce a SalesListingID property for the value and use the following mapping instead:
modelBuilder.Entity<SalesListing>().HasMany( e => e.Descriptions ).WithRequired()
.HasForeignKey( r => r.SalesListingID );