Unable to create a constant value of type. Only use primitive types - entity-framework

I've read some other posts but they have not helped.
CarPart is an EF4 gened class
[EdmEntityTypeAttribute(NamespaceName="xxxx.Data.Domain.Model", Name="CarPart")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class CarPart : EntityObject
{
#region Factory Method
/// summary>
/// Create a new CarPart object.
/// </summary>
/// <param name="carPartId">Initial value of the CarPartId property.</param>
/// <param name="name">Initial value of the Name property.</param>
/// <param name="carPartTypeId">Initial value of the CarPartId property.</param>
public static CarPart CreateCarPart(global::System.Int32 carPartId, global::System.String name, global::System.Int32 carPartId)
{
CarPart carPart = new CarPart();
carPart.CarPartId = carPartId;
carPart.Name = name;
carPart.CarPartTypeId = carPartTypeId;
return carPart;
}
#endregion
#region Primitive Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 CarPartId
{
get
{
return _CarPartId;
}
set
{
if (_CarPartId != value)
{
OnCarPartIdChanging(value);
ReportPropertyChanging("CarPartId");
_CarPartId = StructuralObject.SetValidValue(value);
ReportPropertyChanged("CarPartIdId");
OnCarPartIdChanged();
}
}
}
private global::System.Int32 _CarPartId;
partial void OnCarPartIdChanging(global::System.Int32 value);
partial void OnCarPartIdChanged();
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String Name
{
get
{
return _Name;
}
set
{
OnNameChanging(value);
ReportPropertyChanging("Name");
_Name = StructuralObject.SetValidValue(value, false);
ReportPropertyChanged("Name");
OnNameChanged();
}
}
private global::System.String _Name;
partial void OnNameChanging(global::System.String value);
partial void OnNameChanged();
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 CarPartTypeId
{
get
{
return _CarPartTypeId;
}
set
{
OnCarPartTypeIdChanging(value);
ReportPropertyChanging("CarPartTypeId");
_CarPartTypeId = StructuralObject.SetValidValue(value);
ReportPropertyChanged("CarPartTypeId");
OnCarPartTypeIdChanged();
}
}
private global::System.Int32 _CarPartTypeId;
partial void OnCarPartTypeIdChanging(global::System.Int32 value);
partial void OnCarPartTypeIdChanged();
#endregion
#region Navigation Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("xxxx.Data.Domain.Model", "FK_CarPartId", "Part")]
public EntityCollection<Part> Parts
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Part>("xxxxx.Data.Domain.Model.FK_CarPartId", "Part");
}
set
{
if ((value != null))
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Part>("xxxx.Data.Domain.Model.FK_CarPartId", "Part", value);
}
}
}
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("xxxxx.Data.Domain.Model", "FK_CarPartTypeId", "CarPartType")]
public CarPartType CarPartType
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<CarPartType>("xxxx.Data.Domain.Model.FK_CarPartTypeId", "CarPartType").Value;
}
set
{
((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<CarPartType>("xxxxx.Data.Domain.Model.FK_CarPartTypeId", "CarPartType").Value = value;
}
}
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[BrowsableAttribute(false)]
[DataMemberAttribute()]
public EntityReference<CarPartType> CarPartTypeReference
{
get
{
return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<CarPartType>("xxxx.Data.Domain.Model.FK_CarPartTypeId", "CarPartType");
}
set
{
if ((value != null))
{
((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<CarPartType>("xxxxxx.Data.Domain.Model.FK_CarPartTypeId", "CarPartType", value);
}
}
}
#endregion
}
So here's my join code:
List<Parts> parts = _context.Parts.Where(p => p.PartId == partId).ToList();
List<CarParts> parts = _context.CarParts
.Join(parts, cp => cp.PartId, p => p.PartId, (cp, p) => cp).ToList();
Error: Unable to create a constant value of type 'Model.CarParts'. Only
primitive types ('such as Int32, String, and Guid') are supported in
this context.
Tried to look at it but can't get past this. I'm a bit new to LINQ-To-SQL..done a fair amount but not a ton (mostly done LINQ to Objects) so new to joins with this.

if i'm understanding right you want to do something like this:
internal class Db
{
public Db()
{
var parts = new List<Part>
{
new Part() {PartId = 1, PartName = "Part 1"},
new Part() {PartId = 2, PartName = "Part 2"},
};
Parts = parts.AsQueryable();
var carParts = new List<CarPart>
{
new CarPart() {CarPartId = 1, CarPartName = "Car Part 1.1", PartId = 1},
new CarPart() {CarPartId = 1, CarPartName = "Car Part 1.2", PartId = 1},
new CarPart() {CarPartId = 1, CarPartName = "Car Part 2.1", PartId = 2},
};
CarParts = carParts.AsQueryable();
}
public IQueryable<Part> Parts { get; set; }
public IQueryable<CarPart> CarParts { get; set; }
}
internal class CarPart
{
public int CarPartId { get; set; }
public string CarPartName { get; set; }
public int PartId { get; set; }
}
internal class Part
{
public int PartId { get; set; }
public string PartName { get; set; }
}
static void Main(string[] args)
{
Db db = new Db();
var result = from carPart in db.CarParts
join part in db.Parts on carPart.PartId equals part.PartId
select new {Part = part, CarPart = carPart};
var lambdaResult = db.CarParts.Join(db.Parts, part => part.PartId, caPart => caPart.PartId,
(carPart, part) => new {CarPart = carPart, Part = part});
foreach (var item in result)
{
Console.WriteLine(item.Part.PartName);
Console.WriteLine(item.CarPart.CarPartName);
}
Console.WriteLine("------------");
foreach (var item in lambdaResult)
{
Console.WriteLine(item.Part.PartName);
Console.WriteLine(item.CarPart.CarPartName);
}
}
the result is a new anonymous object with the joined objects as content.
it would print this to the console:
Part 1
Car Part 1.1
Part 1
Car Part 1.2
Part 2
Car Part 2.1
-------
Part 1
Car Part 1.1
Part 1
Car Part 1.2
Part 2
Car Part 2.1

Related

Entity Framework 6 - Field conditional mapping

I am using Entity Framework for mapping our company software' and what i would like create a sort of conditional field mapping based on some field attributes i have created. Trying to better explain:
I have created the attribute ModelPersistentAttribute that I use in the following way:
[ModelPersistent("WORKORDER", persistentSchema: "mySchema", fromVersion: "0.0", toVersion: "2.0")]
public class WorkOrderDTO : AMOSEntityDTO
{
public WorkOrderDTO()
{ }
public WorkOrderDTO(decimal WORKORDERID, string WONO, DateTime LASTUPDATED)
{
this.WORKORDERID = WORKORDERID;
this.WONO = WONO;
this.LASTUPDATED = LASTUPDATED;
}
[Key]
public decimal WORKORDERID { get; set; }
public string WONO { get; set; }
[ModelPersistentAttribute(persistentName: "TITLE", fromVersion:"0.0")]
public string myTITLE { get; set; }
}
Then I created a mapping class that has an automap method which is the following
/// <summary>
/// Automapping
/// </summary>
/// <param name="configurationOptions"></param>
protected void AutoMap(EntityTypeMapCondigurationOptionsEFNet<TEntity> entityConfigurationOptions)
{
IEnumerable<ModelPersistentAttribute> persistentAttributes = typeof(TEntity).GetCustomAttributes<ModelPersistentAttribute>();
if (persistentAttributes.Count() == 0 || persistentAttributes.Any(mpa => mpa.IsInRange(entityConfigurationOptions.Version, entityConfigurationOptions.DBMSType)))
{
if (persistentAttributes.Count() != 0) MapEntity(entityConfigurationOptions, persistentAttributes);
foreach (var prop in typeof(TEntity).GetProperties())
{
NotMappedAttribute notMapped = prop.GetCustomAttribute<NotMappedAttribute>();
if (notMapped == null)
{
IEnumerable<ModelPersistentAttribute> modelAttributes = prop.GetCustomAttributes<ModelPersistentAttribute>();
if (modelAttributes.Count() == 0 || (modelAttributes.Any(ma => ma.IsInRange(entityConfigurationOptions.Version))))
MapProperty(prop, entityConfigurationOptions, modelAttributes);
else
IgnoreProperty(prop, entityConfigurationOptions);
}
}
}
else
IgnoreEntity(entityConfigurationOptions);
}
Everything seems to work properly for mapping
protected virtual void MapProperty(PropertyInfo propertyInfo, EntityTypeMapCondigurationOptionsEFNet<TEntity> entityConfigurationOptions, IEnumerable<ModelPersistentAttribute> modelAttributes)
{
ModelPersistentAttribute modelVersionAttribute = modelAttributes.FirstOrDefault<ModelPersistentAttribute>(mpa => mpa.IsInRange(entityConfigurationOptions.Version, entityConfigurationOptions.DBMSType));
string persistentName = string.Empty;
if (modelVersionAttribute != null)
persistentName = modelVersionAttribute.PersistentName;
else
persistentName = propertyInfo.Name;
entityConfigurationOptions.ModelBuilder.Properties().Where(p => p.Equals(propertyInfo)).Configure(c=> c.HasColumnName(persistentName));
}
but I am not able to run it when i want to ignore a property:
/// <summary>
/// Ignore property
/// </summary>
/// <param name="propertyInfo"></param>
protected virtual void IgnoreProperty(PropertyInfo propertyInfo, EntityTypeMapCondigurationOptionsEFNet<TEntity> entityConfigurationOptions)
{
entityConfigurationOptions.ModelBuilder.Types<TEntity>().Configure(ctc => ctc.Ignore(p => propertyInfo));
// The following as well doesn't work
//entityConfigurationOptions.ModelBuilder.Types<TEntity>//().Configure(ctc => ctc.Ignore(p => propertyInfo.Name));
}
The meaning of the error I get is something that I understand but I don't know how to solve:
The expression 'p => value(DTO.Service.EFCore.EntityTypeVersionMap`1+<>c__DisplayClass4_0[TestDTO.Shared.WorkOrderDTO]).propertyInfo' is not a valid property expression. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'.'
Any help on this will be VERY appreciated!
Thanks in advance
Luigi
You can use the non generic Types method with Where, which Configure -> Ignore method has overloads for string propertyName and PropertyInfo propertyInfo (the one that you need):
entityConfigurationOptions.ModelBuilder
.Types().Where(type => type == typeof(TEntity))
.Configure(ctc => ctc.Ignore(propertyInfo));

Pass variable data to ValidationAttribute

I have a requirement to apply different min length values in a change password function to a new password being created based on a user's roles. If a user has no administrative roles min length is 12, if they have admin roles min length is 16.
The current code has no such variable requirement logic. The implementation for the new password property was like so in model class called ChangePasswordData:
///summary>
/// Gets and sets the new Password.
/// </summary>
[Display(Order = 3, Name = "NewPasswordLabel", ResourceType = typeof(UserResources))]
[Required]
[PasswordSpecialChar]
[PasswordMinLower]
[PasswordMinUpper]
[PasswordMaxLength]
[PasswordMinLength]
[PasswordMinNumber]
public string NewPassword { get; set; }
The validation attribute is set like so:
/// <summary>
/// Validates Password meets minimum length.
/// </summary>
public class PasswordMinLength : ValidationAttribute
{
public int MinLength { get; set; }
public bool IsAdmin { get; set; }
public PasswordMinLength()
{
// Set this here so we override the default from the Framework
this.ErrorMessage = ValidationErrorResources.ValidationErrorBadPasswordLength;
//Set the default Min Length
this.MinLength = 12;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value == null || !(value is string) || !UserRules.PasswordMinLengthRule(value.ToString(), this.MinLength))
{
return new ValidationResult(this.ErrorMessageString, new string[] { validationContext.MemberName });
}
return ValidationResult.Success;
}
}
I want to be able to set the value of MinLength to 12 or 16 based on the value of IsAdmin however i cannot figure out how to decorate the attribute [PasswordMinLength(IsAdmin=myvarable)]. Only constants are allowed. How can I inject a property value into the ValidationAttribute that I can evaluate to determine the correct minimum length?
Thanks!
Thanks to Steve Greene for providing this link to an example (http://odetocode.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-code.aspx), I have the answer to my validation question. Here is the updated code:
/// <summary>
/// Validates Password meets minimum length.
/// </summary>
public class PasswordMinLength : ValidationAttribute
{
public int MinLength { get; set; }
public PasswordMinLength(string IsAdminName)
{
// Set this here so we override the default from the Framework
this.ErrorMessage = ValidationErrorResources.ValidationErrorBadPasswordLength;
IsAdminPropertyName = IsAdminName;
//Set the default Min Length
this.MinLength = 12;
}
public string IsAdminPropertyName{ get; set; }
public override string FormatErrorMessage(string name)
{
return string.Format(ErrorMessageString, name, IsAdminPropertyName);
}
protected bool? GetIsAdmin(ValidationContext validationContext)
{
var retVal = false;
var propertyInfo = validationContext
.ObjectType
.GetProperty(IsAdminPropertyName);
if (propertyInfo != null)
{
var adminValue = propertyInfo.GetValue(
validationContext.ObjectInstance, null);
return adminValue as bool?;
}
return retVal;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (GetIsAdmin(validationContext) != null)
{
if (GetIsAdmin(validationContext) == true)
this.MinLength = 16;
else
this.MinLength = 12;
}
else
this.MinLength = 12;
if (value == null || !(value is string) || !UserRules.PasswordMinLengthRule(value.ToString(), this.MinLength))
{
return new ValidationResult(this.ErrorMessageString, new string[] { validationContext.MemberName });
}
return ValidationResult.Success;
}
}
I simply added an IsAdmin property to my Model class and decorated the PasswordMinLength attribute like so:
[Display(Order = 3, Name = "NewPasswordLabel", ResourceType = typeof(UserResources))]
[Required]
[PasswordSpecialChar]
[PasswordMinLower]
[PasswordMinUpper]
[PasswordMaxLength]
[PasswordMinLength("IsAdmin")]
[PasswordMinNumber]
public string NewPassword { get; set; }
public bool IsAdmin { get; set; }
Works like a charm. Thanks Steve!

Entity framework Generic query in Nongeneric Property

In Entity framework I have objectsets like
public partial class Building
{
public int BuildingID { get; set; }
public string BuildingName { get; set; }
}
public partial class Town
{
public int TownID { get; set; }
public string TownName { get; set; }
}
I want to create a generic query like
T.OrderBy(o=>o.Id).Skip(maxDispItem * (page - 1)).Take(maxDispItem).ToList();
T is generic class can be Building or Town but problem is BuildingId and TownId has different name.I don't want to change their name as Id and create interface IIdentity.
Maybe you could try something like this:
var query = (typeof(T) == typeof(Building) ?
context.Buildings.Select(b => new { Id = b.BuildingId, Name = b.BuildingName }) :
context.Towns.Select(t => new { Id = t.TownId, Name = b.TownName }))
.OrderBy(o => o.Id)...
Not tested but that's worth a test...
You can create generic method which find a field decorated with KeyAttribute, and then performs sorting by found key field. I have tested your model, works perfectly. Look at code snippet.
DbContext:
using System.Collections.Generic;
using System.Data.Entity;
namespace ConsoleApplication28.Entities
{
public class AppDbContext : DbContext
{
public AppDbContext()
{
Database.Connection.ConnectionString = #"Data Source=NOTEBOOK-PC;Initial Catalog=StackOverflowTest;Integrated Security=True";
Database.SetInitializer(new AppDbInitializer());
}
public DbSet<Town> Towns { get; set; }
public DbSet<Building> Buildings { get; set; }
}
public class AppDbInitializer : DropCreateDatabaseIfModelChanges<AppDbContext>
{
protected override void Seed(AppDbContext context)
{
context.Buildings.AddRange(new List<Building>
{
new Building {BuildingName = "Building1"},
new Building {BuildingName = "Building2"},
});
context.Towns.AddRange(new List<Town>
{
new Town {TownName = "Town1"},
new Town {TownName = "Town2"},
});
context.SaveChanges();
base.Seed(context);
}
}
}
Building
using System.ComponentModel.DataAnnotations;
namespace ConsoleApplication28.Entities
{
public class Building
{
[Key]
public int BuildingID { get; set; }
public string BuildingName { get; set; }
}
}
Town
using System.ComponentModel.DataAnnotations;
namespace ConsoleApplication28.Entities
{
public class Town
{
[Key]
public int TownID { get; set; }
public string TownName { get; set; }
}
}
Program
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ConsoleApplication28.Entities;
using System.ComponentModel.DataAnnotations;
namespace ConsoleApplication28
{
class Program
{
static void Main(string[] args)
{
const int maxDispItem = 10;
const int page = 1;
var db = new AppDbContext();
var towns = db.Towns.OrderByKey().Skip(maxDispItem * (page - 1)).Take(maxDispItem).ToList();
var buildings = db.Buildings.OrderByKey().Skip(maxDispItem * (page - 1)).Take(maxDispItem).ToList();
}
}
public static class Extensions
{
/// <summary>
/// Sorts the elements of a sequence in ascending order according to a key specified using KeyAttribute
/// </summary>
public static IOrderedQueryable<T> OrderByKey<T>(this IQueryable<T> source, bool isAsc = true)
{
var type = typeof(T);
var keyProperty = type.GetProperties().Single(x => x.GetCustomAttributes(typeof(KeyAttribute)).Any());
return source.OrderBy(keyProperty.Name, isAsc);
}
#region COPIED FROM THERE http://stackoverflow.com/questions/41244/dynamic-linq-orderby-on-ienumerablet
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property, bool isAsc)
{
return isAsc ? source.OrderBy(property) : source.OrderByDescending(property);
}
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);
object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
#endregion
}
}

Entity Framework: Entity with composite key as PK/FK throws exception

On escalado, throws the exception. It throws with or wihtout Include.
static void Main(string[] args)
{
try
{
using (var context = new CKContext())
{
var servReprosWithIncludes = context.ServicioRepro
.Include(p => p.Categoria)
.ToList();
var escalado = context.EscaladoPrecio
//.Include(p => p.Servicio)
.ToList();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
InvalidOperationException: The value of a property that is part of an object's key does not match the corresponding property value stored in the ObjectContext. This can occur if properties that are part of the key return inconsistent or incorrect values or if DetectChanges is not called after changes are made to a property that is part of the key.
The mapping of EscaladoPrecio:
public class EscaladoPrecioMapping : EntityTypeConfiguration<EscaladoPrecio>
{
public EscaladoPrecioMapping()
{
base.HasKey(p => new { p.Desde, p.Hasta, p.ServicioReproId });
base.HasRequired(p => p.Servicio)
.WithMany()
.HasForeignKey(p => p.ServicioReproId);
base.ToTable("PreciosServicioReprografia");
}
}
The entity ServicioRepro is a part from TPT hierarchy. Looks like:
public class ServicioRepro : Producto
{
public bool IncluirPrecioClick { get; set; }
public bool IncluirPrecioPapel { get; set; }
public bool HayPapel { get; set; }
public bool HayImpresion { get; set; }
public bool PrecioPorVolumen { get; set; }
//public virtual ICollection<EscaladoPrecio> EscaladoPrecio { get; set; }
public virtual CategoriaServicioRepro Categoria { get; set; }
public virtual ServicioReproFacturacionType ServicioReproFacturacionType { get; set; }
}
On this entity you can't see the key, because the base entity Producto have it.
The entity EscaladoPrecio have 3 PK: desde, hasta and Servicio. Servicio is PK and FK.
The entity looks like (methods, overrides and members have been removed to reduce the code):
public class EscaladoPrecio : IComparable<EscaladoPrecio>, IComparable<int>, IComparable, IEntity
{
#region Declarations
private int _desde;
private int _hasta;
private double _precio;
private int _cada;
#endregion Declarations
#region Constructor
public EscaladoPrecio()
: this(1, 1, 0, 0)
{ }
public EscaladoPrecio(int desde, int hasta, double precio)
: this(desde, hasta, precio, 0)
{ }
public EscaladoPrecio(int desde, int hasta, double precio, int cada)
{
_desde = desde;
_hasta = hasta;
_precio = precio;
_cada = cada;
}
#endregion Constructor
#region Properties
public int Desde
{
get
{
return _desde;
}
set
{
_desde = value;
}
}
public int Hasta
{
get
{
return _hasta;
}
set
{
_hasta = value;
}
}
public double Precio
{
get
{
return _precio;
}
set
{
_precio = value;
}
}
public int Cada
{
get
{
return _cada;
}
set
{
_cada = value;
}
}
#endregion Properties
private int _ServicioReproId;
public int ServicioReproId
{
get
{
if (Servicio != null)
{
_ServicioReproId = Servicio.Id;
return Servicio.Id;
}
else
return 0;
}
set
{
_ServicioReproId = value;
}
}
public virtual ServicioRepro Servicio { get; set; }
}
Why throws the exception?
Why are you doing this:
public int ServicioReproId
{
get
{
if (Servicio != null)
{
_ServicioReproId = Servicio.Id;
return Servicio.Id;
}
else
return 0;
}
set
{
_ServicioReproId = value;
}
}
Your part of the key property ServicioReproId is returning 0 here potentially although it has been loaded (and stored in the context) with a value != 0 (probably). I think this part of the exception is refering to this problem: "This can occur if properties that are part of the key return inconsistent or incorrect values."
Better leave it an automatic property:
public int ServicioReproId { get; set; }
try to initialice his virtual property in the constructor of the class EscaladoPrecio()

Entity Framework options to map list of strings or list of int (List<string>)

I want to store an object that contains a List of primitives using EF.
public class MyObject {
public int Id {get;set;}
public virtual IList<int> Numbers {get;set;}
}
I know that EF cannot store this, but I'd like to know possible solutions to solve this problem.
The 2 Solutions I can think of are:
1.Create a Dummy object that has an Id and the Integervalue, e.g.
public class MyObject {
public int Id {get;set;}
public virtual IList<MyInt> Numbers {get;set;}
}
public class MyInt {
public int Id {get;set;}
public int Number {get;set;}
}
2.Store the list values as a blob, e.g.
public class MyObject {
public int Id {get;set;}
/// use NumbersValue to persist/load the list values
public string NumbersValue {get;set;}
[NotMapped]
public virtual IList<int> Numbers {
get {
return NumbersValue.split(',');
}
set {
NumbersValue = value.ToArray().Join(",");
}
}
}
The Problem with the 2. approach is, that I have to create a Custom IList implementation to keep track if someone modifies the returned collection.
Is there a better solution for this?
Although I do not like to answer my own question, but here is what solved my problem:
After I found this link about Complex Types I tried several implementations, and after some headache I ended up with this.
The List values get stored as a string on the table directly, so it's not required to perform several joins in order to get the list entries. Implementors only have to implement the conversation for each list entry to a persistable string (see the Code example).
Most of the code is handled in the Baseclass (PersistableScalarCollection). You only have to derive from it per datatype (int, string, etc) and implement the method to serialize/deserialize the value.
It's important to note, that you cannot use the the generic baseclass directly (when you remove the abstract). It seems that EF cannot work with that. You also have to make sure to annotate the derived class with the [ComplexType] attribute.
Also note that it seems not to be possible to implement a ComplexType for IList<T> because EF complains about the Indexer (therefore I went on with ICollection).
It's also important to note, that since everything is stored within one column, you cannot search for values in the Collection (at least on the database). In this case you may skip this implementation or denormalize the data for searching.
Example for a Collection of integers:
/// <summary>
/// ALlows persisting of a simple integer collection.
/// </summary>
[ComplexType]
public class PersistableIntCollection : PersistableScalarCollection<int> {
protected override int ConvertSingleValueToRuntime(string rawValue) {
return int.Parse(rawValue);
}
protected override string ConvertSingleValueToPersistable(int value) {
return value.ToString();
}
}
Usage example:
public class MyObject {
public int Id {get;set;}
public virtual PersistableIntCollection Numbers {get;set;}
}
This is the baseclass that handles the persistence aspect by storing the list entries within a string:
/// <summary>
/// Baseclass that allows persisting of scalar values as a collection (which is not supported by EF 4.3)
/// </summary>
/// <typeparam name="T">Type of the single collection entry that should be persisted.</typeparam>
[ComplexType]
public abstract class PersistableScalarCollection<T> : ICollection<T> {
// use a character that will not occur in the collection.
// this can be overriden using the given abstract methods (e.g. for list of strings).
const string DefaultValueSeperator = "|";
readonly string[] DefaultValueSeperators = new string[] { DefaultValueSeperator };
/// <summary>
/// The internal data container for the list data.
/// </summary>
private List<T> Data { get; set; }
public PersistableScalarCollection() {
Data = new List<T>();
}
/// <summary>
/// Implementors have to convert the given value raw value to the correct runtime-type.
/// </summary>
/// <param name="rawValue">the already seperated raw value from the database</param>
/// <returns></returns>
protected abstract T ConvertSingleValueToRuntime(string rawValue);
/// <summary>
/// Implementors should convert the given runtime value to a persistable form.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
protected abstract string ConvertSingleValueToPersistable(T value);
/// <summary>
/// Deriving classes can override the string that is used to seperate single values
/// </summary>
protected virtual string ValueSeperator {
get {
return DefaultValueSeperator;
}
}
/// <summary>
/// Deriving classes can override the string that is used to seperate single values
/// </summary>
protected virtual string[] ValueSeperators {
get {
return DefaultValueSeperators;
}
}
/// <summary>
/// DO NOT Modeify manually! This is only used to store/load the data.
/// </summary>
public string SerializedValue {
get {
var serializedValue = string.Join(ValueSeperator.ToString(),
Data.Select(x => ConvertSingleValueToPersistable(x))
.ToArray());
return serializedValue;
}
set {
Data.Clear();
if (string.IsNullOrEmpty(value)) {
return;
}
Data = new List<T>(value.Split(ValueSeperators, StringSplitOptions.None)
.Select(x => ConvertSingleValueToRuntime(x)));
}
}
#region ICollection<T> Members
public void Add(T item) {
Data.Add(item);
}
public void Clear() {
Data.Clear();
}
public bool Contains(T item) {
return Data.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex) {
Data.CopyTo(array, arrayIndex);
}
public int Count {
get { return Data.Count; }
}
public bool IsReadOnly {
get { return false; }
}
public bool Remove(T item) {
return Data.Remove(item);
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator() {
return Data.GetEnumerator();
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator() {
return Data.GetEnumerator();
}
#endregion
}
I'm using EF Core and had a similar problem but solved it in a simpler way.
The idea is to store the list of integers as a comma separated string in the database. I do that by specifying a ValueConverter in my entity type builder.
public class MyObjectBuilder : IEntityTypeConfiguration<MyObject>
{
public void Configure(EntityTypeBuilder<MyObject> builder)
{
var intArrayValueConverter = new ValueConverter<int[], string>(
i => string.Join(",", i),
s => string.IsNullOrWhiteSpace(s) ? new int[0] : s.Split(new[] { ',' }).Select(v => int.Parse(v)).ToArray());
builder.Property(x => x.Numbers).HasConversion(intArrayValueConverter);
}
}
More information can be found here: https://entityframeworkcore.com/knowledge-base/37370476/how-to-persist-a-list-of-strings-with-entity-framework-core-
Bernhard's answer is brilliant. I just couldn't help but refine it a little. Here's my two cents:
[ComplexType]
public abstract class EFPrimitiveCollection<T> : IList<T>
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual int Id { get; set; }
const string DefaultValueSeperator = "|";
readonly string[] DefaultValueSeperators = new string[] { DefaultValueSeperator };
[NotMapped]
private List<T> _data;
[NotMapped]
private string _value;
[NotMapped]
private bool _loaded;
protected virtual string ValueSeparator => DefaultValueSeperator;
protected virtual string[] ValueSeperators => DefaultValueSeperators;
[ShadowColumn, MaxLength]
protected virtual string Value // Change this to public if you prefer not to use the ShadowColumnAttribute
{
get => _value;
set
{
_data.Clear();
_value = value;
if (string.IsNullOrWhiteSpace(_value))
return;
_data = _value.Split(ValueSeperators, StringSplitOptions.None)
.Select(x => ConvertFromString(x)).ToList();
if (!_loaded) _loaded = true;
}
}
public EFPrimitiveCollection()
{
_data = new List<T>();
}
void UpdateValue()
{
_value = string.Join(ValueSeparator.ToString(),
_data.Select(x => ConvertToString(x))
.ToArray());
}
public abstract T ConvertFromString(string value);
public abstract string ConvertToString(T value);
#region IList Implementation
public int Count
{
get
{
EnsureData();
return _data.Count;
}
}
public T this[int index]
{
get
{
EnsureData();
return _data[index];
}
set
{
EnsureData();
_data[index] = value;
}
}
public bool IsReadOnly => false;
void EnsureData()
{
if (_loaded)
return;
if (string.IsNullOrWhiteSpace(_value))
return;
if (_data.Count > 0) return;
if (!_loaded) _loaded = true;
_data = _value.Split(ValueSeperators, StringSplitOptions.None)
.Select(x => ConvertFromString(x)).ToList();
}
public void Add(T item)
{
EnsureData();
_data.Add(item);
UpdateValue();
}
public bool Remove(T item)
{
EnsureData();
bool res = _data.Remove(item);
UpdateValue();
return res;
}
public void Clear()
{
_data.Clear();
UpdateValue();
}
public bool Contains(T item)
{
EnsureData();
return _data.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
EnsureData();
_data.CopyTo(array, arrayIndex);
}
public int IndexOf(T item)
{
EnsureData();
return _data.IndexOf(item);
}
public void Insert(int index, T item)
{
EnsureData();
_data.Insert(index, item);
UpdateValue();
}
public void RemoveAt(int index)
{
EnsureData();
_data.RemoveAt(index);
UpdateValue();
}
public void AddRange(IEnumerable<T> collection)
{
EnsureData();
_data.AddRange(collection);
UpdateValue();
}
public IEnumerator<T> GetEnumerator()
{
EnsureData();
return _data.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
EnsureData();
return _data.GetEnumerator();
}
#endregion
}
With that base class you can have as many derivations as you like:
[ComplexType]
public class EFIntCollection : EFPrimitiveCollection<int>
{
public override int ConvertFromString(string value) => int.Parse(value);
public override string ConvertToString(int value) => value.ToString();
}
[ComplexType]
public class EFInt64Collection : EFPrimitiveCollection<long>
{
public override long ConvertFromString(string value) => long.Parse(value);
public override string ConvertToString(long value) => value.ToString();
}
[ComplexType]
public class EFStringCollection : EFPrimitiveCollection<string>
{
string _separator;
protected override string ValueSeparator => _separator ?? base.ValueSeparator;
public override string ConvertFromString(string value) => value;
public override string ConvertToString(string value) => value;
public EFStringCollection()
{
}
public EFStringCollection(string separator)
{
_separator = separator;
}
}
EFPrimitiveCollection works just like a list, so you shouldn't have any issues using it like a normal List. Also the data is loaded on demand.
Here's an example:
if (store.AcceptedZipCodes == null)
store.AcceptedZipCodes = new EFStringCollection();
store.AcceptedZipCodes.Clear();
store.AcceptedZipCodes.AddRange(codes.Select(x => x.Code));
Shadow Column
This attribute is being used to abstract away the Value property. If you do not see the need to do this, simply remove it and make the Value property public.
More information can be found on the ShadowColumnAttribute in my answer here
The simple solution would be in your DataContext (a class that implements DbContext) add this override:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<MyObject>()
.Property(p => p.Numbers)
.HasConversion(
toDb => string.Join(",", toDb),
fromDb => fromDb.Split(',').Select(Int32.Parse).ToList() ?? new List<int>());
}