Automapper with Column Order Data Annotation - ef-code-first

I am having an issue on application start up when I setup my AutoMapper config. It is throwing an exception when creating the mapping for a business object to a data object. The issue appears to be coming from the use of data annotations. It is worth mentioning that the mapping from data object to business object works just fine.
The exception that I get is a CustomAttributeException:
'Order' property specified was not found.
AutoMapper.config Mapping:
Mapper.CreateMap<Note, NoteData>();
The database object is defined as:
public class NoteData
{
[Key]
[Column(Order = -1)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual Guid Id { get; set; }
[Timestamp]
[ConcurrencyCheck]
[Column(Order = 999)]
public virtual byte[] Version { get; set; }
[Required]
public virtual DateTime Date { get; set; }
[Required]
[StringLength(500)]
public virtual string Value { get; set; }
public virtual bool IsDeleted { get; set; }
[Required]
public virtual UserData CreatedBy { get; set; }
}
I have tried ignoring the fields that have Column Order data annotations on them but that did not resolve the issue.
When I comment out the Order data annotations, Automapper has no issues. So my main question is there a way to configure AutoMapper to work with the Column Order data annotations?

Order can't be negative one. And since an answer needs at least 30 characters let me add MSDN's doc:
Gets or sets the zero-based order of the column the property is mapped to.

Related

Entity Framework Core - optional foreign key

I am creating a web api that needs to return details about vehicles. The first part works fine, just returning data from my vehicles table. Now I have another table which may or may not contain additional data about vehicles in the first table. So when I get vehicle data, I want all of the vehicle data, and any additional data from the second table if it exists, like a left join in SQL.
Here are my classes (very much abridged for readability):
public class Vehicle
{
[Key]
[Required]
public string registrationNo { get; set; }
public string capacity{ get; set; }
public string maxGross{ get; set; }
}
public class VehicleDvlaDetail
{
[ForeignKey("Vehicle")]
public string? registrationNumber { get; set; }
public int? co2Emissions { get; set; }
}
And in my context class OnModelCreating I have (again, very abridged):
modelBuilder.Entity<Vehicle>(entity =>
{
entity.HasOne(dvlaRec => dvlaRec.dvlaDetail).WithMany().HasForeignKey(dvla => dvla.registrationNo);
});
This works fine when there is an associated record in the DVLA table, but that isn't always the case. I need to keep them as separate entities as my API will be required to return details from the DVLA table separately as well. Is there any way to create an optional foreign key, as clearly, what I am doing is wrong.
Friendly advice:
Primary key as a string is not a good practice because of performance issues when data table has lots of data in it.
It would be better if you create your model like this:
public class Vehicle
{
public long Id { get; set; }
public string RegistrationNo { get; set; }
public string Capacity { get; set; }
public string MaxGross { get; set; }
public List<VehicleDvlaDetail> VehicleDvlaDetails { get; set; }
}
public class VehicleDvlaDetail
{
public long? VehicleId { get; set; }
public int? Co2Emissions { get; set; }
public Vehicle Vehicle { get; set; }
}
Vehicle and VehicleDvlaDetail are now connected without additional code in OnModelCreating method and it is possible to fetch vehicles with details like this (this is assuming you have named properties in dbcontext Vehicles and VehicleDvlaDetails):
_dbContext.Vehicles.Include(x => x.VehicleDvlaDetails).ToList();
Also as foreign key VehicleId is nullable - this allows for vehicles not to have any dvla details.
Wow. I spent about 3 hours looking for the answer, just posted the question and came across this:
Create an optional foreign key using the fluid-API for Entity Framework 7
So simple...

How to map a Point in the entity framework?

I am developing an API in the .net core and using the Framework entity.
My bank already existed and in one of the tables I have a Point type field to store coordinates (Spatials).
I'm not using any automatic approach (Ex: code First, DataBase First ...), I am modeling my classes myself.
To map this Point field I did it as in primitive types, I believe to be wrong, and I'm getting an error:
System.InvalidOperationException: 'The property 'Address.LatLong'
could not be mapped, because it is of type 'Point' which is not a
supported primitive type or a valid entity type. Either explicitly map
this property, or ignore it using the '[NotMapped]' attribute or by
using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.'
public class Address:BaseModel
{
[Required]
[StringLength(30)]
public string Street { get; set; }
[Required]
public int Number { get; set; }
[StringLength(45)]
public string Observation { get; set; }
[Required]
[StringLength(20)]
public string PostalCode { get; set; }
[ForeignKey(nameof(City))]
[Required]
public int CityId { get; set; }
public virtual City City { get; set; }
public Point LatLong { get; set; } //this is the field
}
if you wanna use that point for other address , so u should add something like this to your point Class :
public virtual ICollection<Address> Address{ get; set; }
and if you dont wanna use that point for another adress , u can bring Lat and Long to your adress class.
by the way i think add this public virtual ICollection<Address> Address{ get; set; } should fix your problem

Cannot generate Id using Data annotation (EF6)

I'm new at EF6 and Asp.net MVC5.
There's problem of generating a unique ID automatically when I try to create my entities using the code first approach.
Consider an entity like this:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PostId { get; set; }
[Required]
[MaxLength(100), MinLength(5)]
public string Title { get; set; }
public string Content { get; set; }
public string Thumbnail { get; set; }
public string Description { get; set; }
public DateTime DateCreated { get; set; }
When I put [DatabaseGenerated(DatabaseGeneratedOption.Identity)] on PostId or even remove it. I was always get an exception error like this:
“Cannot insert the value NULL into column ‘PostId'”.
“column does not allow nulls. INSERT fails.
I don't know why EF6's always try to insert a null value to the Id column.
Then later, I found a solution.
I changed
DatabaseGeneratedOption.Identity to DatabaseGeneratedOption.None
and the problem was solved.
However, this solution doesn't seem to work like I expected.
the values inserted to the column are always the same. It's not unique.
With EF core, everything is just simple, I don't need DatabaseGenerated,just leave it to convention. But with EF6, I'm stuck. What I want is the Id field must be unique and increase everytime it inserted to the database.
Can someone please help me this?
I'm new to EF also, but I just create an Id field and it is inserted automatically by SQL Server, such as this model in my current project. The Id field populates as expected when I add new rows:
public class BlogPosts
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string ShortPost { get; set; }
[Required]
public string Post { get; set; }
[Required]
public string Tags { get; set; }
[Required]
public DateTime Updated { get; set; }
}
When you apply DatabaseGeneratedOption.Identity to your key property you are telling EF that the database will generate the identity. From the error it sounds like your database PostId column is not configured as an identity or primary key.
If you manually created the table and columns in the database you should ensure the configuration is at least compatible, or you should use migrations to ensure the database is configured in a good state for your EF model.

Is it OK to declare a DBSet in the context for both a base table and a derived table?

I have a SalesOrder table which inherits from a SalesDocument table using Table Per Type Inheritance
The ( simplified) table classes are;
[Table("SalesDocumentHeaders")]
public abstract class SalesDocumentHeader
{
[ForeignKey("CreatedByUserId")]
public virtual User CreatedBy { get; set; }
[Required]
public int CreatedByUserId { get; set; }
[Required]
public virtual DateTime? DocumentDate { get; set; }
[Required]
public String ReferenceNumber { get; set; }
}
[Table("SalesOrders")]
public class SalesOrder : SalesDocumentHeader
{
[Required]
public String CustomerOrderNumber { get; set; }
public DateTime? DeliverBy { get; set; }
public virtual SortableBindingList<SalesOrderLine> Lines { get; set; }
}
The context contains
public DbSet<SalesOrder> SalesOrders { get; set; }
public DbSet<SalesDocumentHeader> SalesDocumentHeaders { get; set; }
It doesn't strictly need the SalesOrders DBSet, since SalesOrder inherits from SalesDocumentHeader however I find it convenient.
It seems to work OK, but I am worried that there are 2 ways of reaching the same record , am I doing something wrong?
Usually you only need to keep the DBSet for the base table. This helps when you have multiple derived tables (call them A and B) and you need to decide the actual type dynamically.
For example if you have another entity which references type A or B (like a user can have different types of contact information), you can reference the base table and EF will resolve the correct concrete type at runtime. Though of course this adds some extra casting code.

Getting "Cannot insert the value NULL into column" when trying to save with .Add() method using DbContext . Please check my POCO's and save method

Used code first and everything appears to work apart from the below which also worked before when I used ObjectContext and called context.PCBuilds.AddObject(pcBuild) but after switching to DbContext it's giving me the error.
EFDbContext context = new EFDbContext();
public ActionResult Index()
{
PCBuild pcBuild = new PCBuild();
pcBuild.BuildID = 34245;
pcBuild.BuildName = "Test99";
pcBuild.MarkUp = 25;
pcBuild.BDetails = new List<BDetail>();
context.PCBuilds.Add(pcBuild);
//repository.PCBuilds.Attach(pcBuild);
context.SaveChanges();
return View();
}
Giving me the: Cannot insert the value NULL into column 'BuildID', table 'C:\USERS\ADMIN\DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\NEOCART\NEOCART.WEBUI\APP_DATA\NEODBX.MDF.dbo.PCBuilds'; column does not allow nulls. INSERT fails. Where as BuildID was clearly set before the SaveChanges is called. Appears that calling the .Add(pcBuild) doesn't add the populated object for some reason and when savechanges is called it attempts to insert an empty PCBuild ?
Here are the POCO's
public class PCBuild
{
[Key]
public int BuildID { get; set; }
public string BuildName { get; set; }
public string Socket { get; set; }
public decimal? MarkUp {get; set;}
[InverseProperty("PCBuild")]
public virtual ICollection<BDetail> BDetails { get; set; }
}
public class BDetail
{
[Key]
public int LineID { get; set; }
[ForeignKey("PCBuild")]
public int BuildID { get; set; }
[ForeignKey("Product")]
public int ProductID { get; set; }
public bool? IsSelected { get; set; }
[InverseProperty("BDetails")]
public virtual PCBuild PCBuild { get; set; }
[InverseProperty("BDetails")]
public virtual Product Product { get; set; }
}
Use StoreGeneratedAttribute on the PCBuild.BuildID property. It is not only a key but IDENTITY field.
UPDATE
Actually, it should be [DatabaseGenerated(DatabaseGenerationOption.Identity)] annotation. The article linked above describes early CTP version.
UPDATE 2
Wait, the key is being generated by the app, it is not an identity column in database? Change annotation to [DatabaseGenerated(DatabaseGenerationOption.None)], re-create the context and rebuild the application.
I'm not really familiar with the Code First approach, but could it be that when you specify the BuildID as being a [Key] field, it is setting it as an auto identity field in the database?
As such it may be blocking your attempt to write to it.
Try removing the [Key] identifier, then recreate the database. Can you then save the object ok?

Resources