Automapper map by name issue - entity-framework

AUtomapper v8.1.1 / EF6
Context: Using ProjectTo
Why is this not able to automap by naming convention?
I have a source class which has these 2 properties that are of the same type:
public virtual ParcelBillingAccount ParcelBillingAccount_Duties { get; set; }
public virtual ParcelBillingAccount ParcelBillingAccount_Freight { get; set; }
The ParcelBillingAccount type class has this property:
public string BillToParty { get; set; }
As such, I created a viewModel with a property:
public string ParcelBillingAccount_DutiesBillToParty { get; set; }
But I am getting an unmapped property error:
ShippingContainerHeader -> ShippingContainerHeaderViewModel (Destination member list)
Unmapped properties:
ParcelBillingAccount_DutiesBillToParty

Related

Audit changes made to an owned entity Entity Framework Core

I have two entities Employer and Address
public class Employer
{
public int Code { get; set; }
public string Initials { get; set; }
public string Description { get; set; }
public virtual Address Address { get; private set; }
}
public class Address
{
public string Email { get; private set; }
public string City { get; private set; }
public string PostalCode { get; private set; }
public string AddressLine { get; private set; }
}
I have a method to track changes on both entities for audit purposes. I would like to track the changes on the owned type but I get
"System.InvalidOperationException: 'The property 'Address' on entity type 'Employer' is being accessed using the 'Property' method, but is defined in the model as a navigation property. Use either the 'Reference' or 'Collection' method to access navigation properties.'"
Any idea on how I can fix this?

How to change database column name via Fluent API

I have a following code:
public abstract class Entity
{
public virtual int Id { get; set; }
}
public class Category : Entity
{
public string Name { get; set; }
public virtual ICollection<Category> Children { get; set; }
public virtual ICollection<Item> Items { get; set; }
}
public class Item : Entity
{
public string Name { get; set; }
public decimal Price { get; set; }
public DateTime DateCreated { get; set; }
public virtual Category Category { get; set; }
}
This syntax
modelBuilder.Entity<Category>().Property(x => x.Children).HasColumnName("CategoryID");
gives me Severity Code Description Project File Line
Error CS0453 The type 'ICollection' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'StructuralTypeConfiguration.Property(Expression>)'
Q: How I can change database column name via Fluent API?
The collection itself can't be mapped to one column. You should map this as a one-to-many association:
modelBuilder.Entity<Category>()
.HasMany(c => c.Children)
.WithOptional()
.Map(m => m.MapKey("CategoryID"));

Entity Framework One-Many Relationship on Same Entity

I need to define Organizational chart schema in Entity Framework.
PersonelJob Entity model is:
public class PersonelJob : BaseEntity
{
public Int64 ID { get; set; }
public string Name { get; set; }
public Int64? ParentId { get; set; }
public virtual PersonelJob Parent { get; set; }
public virtual ICollection<PersonelJob> Childs { get; set; }
}
As you can see each job could be a job parent and have some job children.
How could map this entity to Database, with Fulent Api?
Override the OnModelCreating method on your context and add this configuration:
modelBuilder.Entity<PersonelJob>()
.HasOptional(pj => pj.Parent)
.WithMany(pj=>pj.Childs)
.HasForeignKey(pj => pj.ParentId);

Passing Generic Class' Property to ColumnAttribute in Code First Migration

I have an abstract class inherited in 3 POCO objects:
public abstract class BaseObject
{
public virtual int Id { get; set; }
}
public class Post : BaseObject
{
public string Name { get; set; }
public virtual ICollection<PostCategory> PostCategory { get; set; }
}
public class Category : BaseObject
{
public string Name { get; set; }
public virtual ICollection<PostCategory> PostCategory { get; set; }
}
public class PostCategory
{
[Key]
[Column("Id", Order = 0)]
public int PostId { get; set; }
[Key]
[Column("Id", Order = 1)]
public int CategoryId { get; set; }
public string Value { get; set; }
public virtual Post Post { get; set; }
public virtual Category Category { get; set; }
}
However, whenever I do 'add-migration' in Package Manager Console, I get error:
Schema specified is not valid. Errors: (30,6) : error 0019: Each
property name in a type must be unique. Property name 'Id' was already
defined.
Basically complaining the ColumnAttribute having same property name (Id property in PostCategory object).
I need the property name to be the same for creating generic class that is used in generic Repo class. That's why I have the Id in an abstract class. But, this gives me error on CF migration part. Is there a way to get around this?
Thanks!
The ColumnAttribute attribute, sets the name generated in the SQL server. Obviously the column Id cannot be generated twice.
Simply remove the ColumnAttributes, allowing the server to generate the PostCategory table peacefully.

Schema specified is not valid. Errors: The relationship not loaded because the type is not available

I have the entities Dependency, Product and Access. Dependency is connected to Product and Access. When i try to create a object set of Access with:
this.context.CreateObjectSet<Access>();
It's working... but when i try to create a object set of Product i get this error: Schema specified is not valid. Errors: The relationship 'Model.FK_Product_Dependency' was not loaded because the type 'Model.Dependency' is not available.
Any ideas?
OBS: i'm working with Database to Model, and with EF 4.0
POCO Entities:
public class Dependency
{
public virtual int Id { get; set; }
public virtual int IdParent { get; set; }
public virtual string Name { get; set; }
public virtual decimal Type { get; set; }
public virtual Dependency Parent { get; set; }
}
public class Product
{
public virtual int Id { get; set; }
public virtual int IdDependency { get; set; }
public virtual decimal Type { get; set; }
public virtual string Name { get; set; }
public virtual string Obs { get; set; }
public virtual Dependency Dependency { get; set; }
}
public class Access
{
public virtual int Id { get; set; }
public virtual int IdProfile { get; set; }
public virtual string Name { get; set; }
public virtual Profile Profile { get; set; }
public virtual ICollection<Dependency> Dependencies { get; set; }
}
Solved. So, the POCO entities cannot be in different namespaces/dlls if access each other. In the exemple above, Dependency and Access was in a namespace/dll and Product in another. I thought that was only necessary the POCO entity be the same (properties and name) like the entity mapped by EF, but is necessary that the entities are in the same namespace/dll (only to the entities that access each other).