Entity Framework Model Mapping With Legacy Database and Constant - sql-server

I'm using EF6 over a DB that's over 15 years old. I did not make this architecture decision. All of my experience with EF has been code-first, with models I've created myself.
One of our tables has a reference table that has some info needed in selects only. These mappings will never be used for update/insert/delete.
I have two entities. My primary table:
public class QualParam
{
[Key]
public string MillId { get; set; }
[Key]
public string Qparam { get; set; }
public string ValueNum { get; set; }
public string ActiveFlag { get; set; }
public int ModifiedTimestamp { get; set; }
public int CreatedTimestamp { get; set; }
public decimal SbIncrement { get; set; }
public string QualityDesc { get; set; }
public string TypeCode { get; set; }
public QualParamHeader QualParamHeader { get; set; }
public virtual UnitMeasure UnitMeasure { get; set; }
}
and a reference table:
public class UnitMeasure
{
[Key]
public string UnitOfMeasure { get; set; }
public int ModifiedTimestamp { get; set; }
public int CreatedTimestamp { get; set; }
public string BaseUnits { get; set; }
public string UnitDesc { get; set; }
[Key]
public string TableName { get; set; }
public string RollWeightFlag { get; set; }
public string MetricFlag { get; set; }
public string MxActionCode { get; set; }
[Key]
public string TypeCode { get; set; }
public byte[] RecordVersion { get; set; }
public List<QualParam> QualParams { get; set; }
}
QualParam may have a UnitMeasure, and UnitMeasure can have many QualParams, easy, right?
In SQL the join is done thusly
SELECT *
FROM qual_params AS q
LEFT JOIN unit_measure AS u
ON u.unit_meas = q.unit_meas
AND u.table_name = 'qual_params'
AND u.type_code = q.type_code
So yes, there's a constant, the table name, and yes the keys from the source to the reference tables don't match, and aren't even enumerated in the db to begin with. Again, legacy.
Our Db context-
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new QualParamsConfiguration());
modelBuilder.Configurations.Add(new UnitMeasureConfiguration());
base.OnModelCreating(modelBuilder);
}
I'm using auto generated configs-
public QualParamConfiguration()
: this("dbo")
{
}
public QualParamConfiguration(string schema)
{
ToTable(schema + ".qual_params");
HasKey(x => new { x.MillId, x.Qparam });
Property(x => x.Qparam).HasColumnName(#"qparam").IsRequired().IsFixedLength().IsUnicode(false).HasColumnType("char").HasMaxLength(10).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None);
Property(x => x.ValueNum).HasColumnName(#"value_num").IsRequired().IsFixedLength().IsUnicode(false).HasColumnType("char").HasMaxLength(9);
Property(x => x.ActiveFlag).HasColumnName(#"active_flag").IsRequired().IsFixedLength().IsUnicode(false).HasColumnType("char").HasMaxLength(1);
Property(x => x.ModifiedTimestamp).HasColumnName(#"ts_modified").IsRequired().HasColumnType("int");
Property(x => x.CreatedTimestamp).HasColumnName(#"ts_create").IsRequired().HasColumnType("int");
Property(x => x.SbIncrement).HasColumnName(#"sb_increment").IsRequired().HasColumnType("decimal").HasPrecision(7, 3);
Property(x => x.QualityDesc).HasColumnName(#"quality_desc").IsRequired().IsFixedLength().IsUnicode(false).HasColumnType("char").HasMaxLength(50);
Property(x => x.MillId).HasColumnName(#"mill_id").IsRequired().IsFixedLength().IsUnicode(false).HasColumnType("char").HasMaxLength(10).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None);
this.HasRequired(a => a.QparamHeader).WithMany(b => b.QualParams).HasForeignKey(c => c.Qparam).WillCascadeOnDelete(false);
}
The last navigation wrote itself, for obvious reasons. Simple relationship.
This one, not so much. I'm open to everything- I'm fairly aware at this point that I'll have to create a separate model to achieve this.
The examples I've seen using .Map must be from older versions of EF, since many of the methods are not available to me. Is it even possible to achieve this relationship?

Related

EF core order by navigation property

I have an entity relationship like this.
public class Provider
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<ProviderPod> ProviderPods { get; set; } = new List<ProviderPod>();
}
public class ProviderPod
{
public int Id { get; set; }
public int ProviderId { get; set; }
public int PodId { get; set; }
public virtual Provider Provider { get; set; }
public virtual Pod Pod { get; set; }
}
public class Pod
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<ProviderPod> ProviderPods { get; set; } = new List<ProviderPod>();
}
I need to order the 'Provider' entity by it's navigation property ProviderPods' "Name" separated by a comma. Something like this
IQueryable<Provider> entityQuery = context.Providers.AsQueryable();
//Need to enter Appropriate query below
entityQuery = entityQuery.OrderByDescending(x => string.Join(", ", x.ProviderPods.Select(y => y.Pod.Name)));
var list = entityQuery.Take(12).ToList();
What would be the best way to achieve this ordering?

Migration conflicts in net core Entity Framework

I have two tables in my SQL Server database - one for categories:
public class Category
{
public int Id { get; set; }
public string Name { get; set; }
public int? ParentId { get; set; }
[ForeignKey("ParentId")]
public Category Parent { get; set; }
}
And then the Assistant table:
public class Assistant
{
public int Id { get; set; }
public int UserId { get; set; }
public DateTime CreatedAtUtc { get; set; }
public Status Status { get; set; }
public int? CategoryId { get; set; }
[ForeignKey("CategoryId")]
public Category Category { get; set; }
}
When I try to make a migration I get this error:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Assistants_Categories_CategoryId". The conflict occurred in database "Pirma
isMsSql", table "dbo.Categories", column 'Id'.
I have no idea why.
Thanks
Fix your classes:
public partial class Assistent
{
[Key]
public int Id { get; set; }
public int UserId { get; set; }
public User User{ get; set; }
public DateTime CreatedAtUtc { get; set; }
public int StatusId { get; set; }
public Status Status { get; set; }
public int? CategoryId { get; set; }
[ForeignKey(nameof(CategoryId))]
[InverseProperty("Assistents")]
public virtual Category Category { get; set; }
}
public partial class Category
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int? ParentId { get; set; }
[ForeignKey(nameof(ParentId))]
[InverseProperty(nameof(Category.InverseParent))]
public virtual Category Parent { get; set; }
[InverseProperty(nameof(Assistent.Category))]
public virtual ICollection<Assistent> Assistents { get; set; }
[InverseProperty(nameof(Category.Parent))]
public virtual ICollection<Category> InverseParent { get; set; }
}
This is db context:
public partial class AssistentsContext : DbContext
{
public AssistentsContext()
{
}
public AssistentsContext(DbContextOptions<AssistentsContext> options)
: base(options)
{
}
public virtual DbSet<Assistent> Assistents { get; set; }
public virtual DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Assistent>(entity =>
{
entity.HasOne(d => d.Category)
.WithMany(p => p.Assistents)
.HasForeignKey(d => d.CategoryId);
});
modelBuilder.Entity<Category>(entity =>
{
entity.HasOne(d => d.Parent)
.WithMany(p => p.InverseParent)
.HasForeignKey(d => d.ParentId);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

EF Core Add-Migration generating extra column with ColumnName1

I have the following entities when I generate migration it creates two columns with name RestrictedCategoryId and RestrictedCategoryId1(FK). How to solve this issue to generate only one column with FK?
Note: I need OrderId in each entity.
`C#
public class Order
{
public Guid Id { get; set; }
public DateTime OrderDate { get; set; }
private List<Category> _categories;
public List<Category> Categories => _categories;
}
public class Category
{
public Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public Guid OrderId { get; set; }
public Order Order { get; set; }
private List<RestrictionCategory> _restrictedCategories;
public List<RestrictionCategory> RestrictedCategories => _restrictedCategories;
}
public class RestrictionCategory
{
public Guid Id { get; set; }
public string Name { get; set; }
public Guid OrderId { get; set; }
public Order Order { get; set; }
public Guid CategoryId { get; set; }
public Category Category { get; set; }
public Guid RestrictedCategoryId { get; set; }
public Category RestrictedCategory { get; set; }
}
public class OrderConfiguration : IEntityTypeConfiguration<Order>
{
public void Configure(EntityTypeBuilder<Order> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.Id).IsRequired();
}
}
public class CategoryConfiguration : IEntityTypeConfiguration<Category>
{
public void Configure(EntityTypeBuilder<Category> builder)
{
builder.HasKey(c => new { c.Id, c.OrderId });
builder.Property(o => o.Id).IsRequired();
builder.Property(o => o.OrderId).IsRequired();
builder.HasMany(c => c.RestrictedCategories).WithOne(cr => cr.Category)
.HasForeignKey(cr => new { cr.CategoryId, cr.OrderId
}).OnDelete(DeleteBehavior.NoAction);
}
}
public class RestrictionCategoryConfiguration : IEntityTypeConfiguration<RestrictionCategory>
{
public void Configure(EntityTypeBuilder<RestrictionCategory> builder)
{
builder.HasKey(c => new { c.Id, c.OrderId });
builder.Property(o => o.Id).IsRequired();
builder.Property(o => o.OrderId).IsRequired();
builder.HasIndex(cr => new { cr.RestrictedCategoryId, cr.OrderId });
}
}
`
The entities resembles to actual ones.
Actually you get two additional columns:
RestrictedCategoryId = table.Column<Guid>(nullable: false),
RestrictedCategoryId1 = table.Column<Guid>(nullable: true), // <--
RestrictedCategoryOrderId = table.Column<Guid>(nullable: true) // <--
Apparently EF Core Foreign Key Conventions doesn't play well with composite keys, so you have to explicitly configure the relationship - similar to what you did for the other relationship, just since your model has no corresponding collection navigation property you have to use HasMany with generic type argument and no parameters, e.g. inside CategoryConfiguration:
builder.HasMany<RestrictionCategory>()
.WithOne(cr => cr.RestrictedCategory)
.HasForeignKey(cr => new { cr.RestrictedCategoryId, cr.OrderId})
.OnDelete(DeleteBehavior.NoAction);

Entity Framework 6 foreign key

I am coding an MVC5 internet application with EF6, and have a question in regards to a foreign key name.
I have a model called MapLocationList that has these two fields:
public int mapLocationListGalleryId { get; set; }
public virtual MapLocationListGallery mapLocationListGallery { get; set; }
When EF creates the table, there is both the following columns:
mapLocationListGalleryId
MapLocationListGallery_Id
Can someone please explain why there are two columns for the MapLocationListGallery foreign key?
Thanks in advance
EDIT
I have changed the name to use an uppercase M, yet the additional column is still there.
Here is my model:
public class MapLocationList : IMapLocationItemWithAssets
{
[Key]
public int Id { get; set; }
[Required]
public string name { get; set; }
public bool enabled { get; set; }
[ScaffoldColumn(false)]
public string mapLocationItemType { get; set; }
[ScaffoldColumn(false)]
public string userName { get; set; }
[ScaffoldColumn(false)]
public DateTime creationDate { get; set; }
[ScaffoldColumn(false)]
public DateTime lastUpdate { get; set; }
public string thumbnailDisplayText { get; set; }
public bool parentIsMapLocation { get; set; }
public int thumbnailAssetId { get; set; }
public virtual Asset thumbnailAsset { get; set; }
public int mapLocationId { get; set; }
public virtual MapLocation mapLocation { get; set; }
public int mapLocationListGalleryId { get; set; }
public virtual MapLocationListGallery mapLocationListGallery { get; set; }
public virtual ICollection<MapLocationListItem> listItems { get; set; }
public MapLocationList()
{
creationDate = DateTime.Now;
lastUpdate = DateTime.Now;
listItems = new List<MapLocationListItem>();
}
}
I also have the following in the OnModelCreating function:
modelBuilder.Entity<MapLocationListGallery>()
.HasRequired(c => c.thumbnailAsset)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<MapLocationList>()
.HasRequired(c => c.thumbnailAsset)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<MapLocationList>()
.HasRequired(c => c.mapLocationListGallery)
.WithMany()
.WillCascadeOnDelete(false);
modelBuilder.Entity<MapLocationListItem>()
.HasRequired(c => c.thumbnailAsset)
.WithMany()
.WillCascadeOnDelete(false);
I use this approach as well and I do not experience this behavior. Probably you need to rename your properties to CamelCase (note the capital M):
public int MapLocationListGalleryId { get; set; }
public virtual MapLocationListGallery MapLocationListGallery { get; set; }
If that doesn't help take a look at the ForeignKeyAttribute here and here.
Edit
I'm not familiar with the fluent api, but I think you could try to set the foreign key explicitly using something like:
modelBuilder.Entity<MapLocationList>()
.HasRequired(c => c.mapLocationListGallery)
.WithMany()
.HasForeignKey(x => x.mapLocationListGalleryId)
.WillCascadeOnDelete(false);
For more info see this article, topic: "Configuring Unconventional Foreign Key Names". Although it's strange this is necessary because your code seems to comply with the Code First convention (with capital M, i.e. the class name).

Better way to create database for a pricing system

I need to create a price table system so I am going to create these three tables in my database.
PricingTable (ID, Name, ServiceID, Style)
PricingTablePackages (ID, PricingTable_ID, Title, Price, PricePerTime, Info, Flag, Link)
PricingTablePackagesFeatures (ID, PricingTablePackages_ID, Feature, Value, MoreInfo)
Here one PriceTable can hold more then one PricingTablePackages and one PricingTablePackage can hold more then one PricingTablePackagesFeature.
Is any way to design a better model? In a single database Table ?
I am going to create a MVC3 Model for those table so what is the best way to do this kind of DB Table in a MVC3 Model?
I would use public virtual variables for 'lazy-loading' values when you need them using Entity Framework:
(variable types may be off depending on exactly what you want for each variable)
public class PricingTablePackages
{
public int ID { get; set; }
public int PricingTableID { get; set; }
public virtual PricingTable PricingTable { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public decimal PricePerTime { get; set; }
public string Info { get; set; }
public bool Flag { get; set; }
public string Link { get; set; }
}
public class PricingTablePackagesFeatures
{
public int ID { get; set; }
public int PricingTableID { get; set; }
public virtual PricingTable PricingTable { get; set; }
public string Feature { get; set; }
public string Value { get; set; }
public string MoreInfo { get; set; }
}
public class PricingTable
{
public int ID { get; set; }
public string Name { get; set; }
public int ServiceID { get; set; }
public virtual Service Service { get; set; } // if there is a Service class
public string Style { get; set; }
}

Resources