Entity Framework, configuration for reading and writing - entity-framework

Given the following database model
I have two fluent entity framework configurations, one that works when I read (the setupFields list is set) and the other for writing, which if I use for reading as well, always comes with an empty SetupFields list
[Table("[BALANCE.SETUP]")]
public class SetupEntity
{
[Key]
public Guid Id { get; set; }
public string Title { get; set; }
public virtual ChassisEntity Chassis { get; set; }
public virtual EventEntity Event { get; set; }
public virtual ICollection<SetupFieldEntity> SetupFields { get; set; }
}
[Table("[BALANCE.SETUP.FIELD]")]
public class SetupFieldEntity
{
[Key]
[Column(Order = 0)]
public Guid SetupId { get; set; }
[Key]
[Column(Order = 1)]
public int Sequence { get; set; }
public string Section { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public virtual SetupEntity Setup { get; set; }
}
(can read)
modelBuilder.Entity<SetupEntity>()
.HasMany(x => x.SetupFields)
.WithRequired(x => x.Setup)
.Map(x => x.MapKey("SETUPID"));
(can write)
modelBuilder.Entity<SetupEntity>()
.HasMany(x => x.SetupFields)
.WithRequired()
.HasForeignKey(x => x.SetupId);
If I use the read configuration to read, this is the error I get:
The column name 'SETUPID' is specified more than once in the SET clause. A column cannot be assigned more than one value in the same SET clause. Modify the SET clause to make sure that a column is updated only once. If the SET clause updates columns of a view, then the column name 'SETUPID' may appear twice in the view definition.
UPDATE 1
Just to make things clear, the models are lazy loaded and I'm explicitly inclucing them, so, when using the first connfiguration the models are set as expected:
if (includeFields)
{
x = x.Include(entity => entity.SetupFields);
}
UPDATE 2
Based on the comments bellow I change the mappings to have just this which works when inserting but when reading the child collection is still null:
modelBuilder.Entity<SetupEntity>().HasMany(x => x.SetupFields);
[Table("[BALANCE.SETUP]")]
public class SetupEntity
{
[Key]
public Guid Id { get; set; }
public string Title { get; set; }
public virtual ChassisEntity Chassis { get; set; }
public virtual EventEntity Event { get; set; }
public virtual ICollection<SetupFieldEntity> SetupFields { get; set; }
}
[Table("[BALANCE.SETUP.FIELD]")]
public class SetupFieldEntity
{
[Key]
[Column(Order = 0)]
public Guid SetupId { get; set; }
[Key]
[Column(Order = 1)]
public int Sequence { get; set; }
public string Section { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public virtual SetupEntity Setup { get; set; }
}
UPDATE 3
Based on the comments I completelly removed the attributes from the entities, but the collection property is still null :(
public class SetupEntity
{
public Guid Id { get; set; }
public string Title { get; set; }
public virtual ChassisEntity Chassis { get; set; }
public virtual EventEntity Event { get; set; }
public virtual ICollection<SetupFieldEntity> SetupFields { get; set; }
}
[Table("[BALANCE.SETUP.FIELD]")]
public class SetupFieldEntity
{
public Guid SetupId { get; set; }
public int Sequence { get; set; }
public string Section { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public virtual SetupEntity Setup { get; set; }
}
modelBuilder.Entity<SetupEntity>()
.HasKey(x => x.Id)
.HasMany(x => x.SetupFields)
.WithRequired(x => x.Setup);
modelBuilder.Entity<SetupFieldEntity>()
.HasKey(x => x.SetupId)
.HasKey(x => x.Sequence);

Related

EF Core More Navigation properties from two tables

I am trying to create navigation properties for two table.
Here is the code.
public class CourseMaster
{
public int Id { get; set; }
public string Name { get; set; }
public int? TeamLeaderId { get; set; }
[ForeignKey("TeamLeaderId")]
public StudentMaster TeamLeader { get; set; }
public int? GroupLeaderId { get; set; }
[ForeignKey("GroupLeaderId")]
public StudentMaster GroupLeader { get; set; }
public virtual ICollection<StudentMaster> Students { get; set; }
}
public class StudentMaster
{
public int id { get; set; }
public string Name { get; set; }
public int FirstSemCourseId { get; set; }
[ForeignKey("FirstSemCourseId")]
public CourseMaster FirstSemCourse { get; set; }
public int SecondSemCourseId { get; set; }
[ForeignKey("SecondSemCourseId")]
public CourseMaster SecondSemCourse { get; set; }
public int ThirdSemCourseId { get; set; }
[ForeignKey("ThirdSemCourseId")]
public CourseMaster ThirdSemCourse { get; set; }
public int CourseMasterId { get; set; }
public CourseMaster Course { get; set; }
}
// Fluent API
modelBuilder.Entity<StudentMaster>()
.HasOne(p => p.Course)
.WithMany(b => b.Students)
.HasForeignKey(p => p.CourseMasterId);
But when i am creating migrations i am getting following error.
Unable to determine the relationship represented by navigation property 'CourseMaster.TeamLeader' of the type 'StudentMaster'. Either manually configure the relationship, or ignore this property from model.
Whether the procedure i am following is right or should i create intermediate class.
or how should i create class.
Any help are appreciated.
Thanks

Invalid column name - one-to-many relation - Entity Framework

I have an entity with two foreign keys only on one of them I get an exception:
Invalid column name LookUpTypId
Both have the same code, but only one of them cause exception
public class Term
{
public Term ()
{
RequestTimes = new HashSet<RequestTime>();
}
public decimal Id { get; set; }
public string AcademicYear { get; set; }
public string Semester { get; set; }
public int? NumberOfDays { get; set; }
public bool Active { get; set; }
public bool Reserve { get; set; }
public string Description { get; set; }
public virtual ICollection<RequestTime> RequestTimes { get; set; }
}
public class LookUp
{
public LookUp()
{
RequestTimes = new HashSet<RequestTime>();
}
public int Id { get; set; }
public string Group { get; set; }
public string Key { get; set; }
public string Value { get; set; }
public virtual ICollection<RequestTime> RequestTimes { get; set; }
}
public class RequestTime
{
public decimal Id { get; set; }
public DateTime FromTime { get; set; }
public DateTime ToTime { get; set; }
public int RequestMaxNumber { get; set; }
public virtual int LookUpTypId { get; set; }
public virtual decimal TermId { get; set; }
[ForeignKey("TermId")]
public virtual Term Term { get; set; }
[ForeignKey("LookUpTypId")]
public virtual LookUp LookUp { get; set; }
}
//relationship in RequestTimeMap
this.HasRequired<LookUp>(rt => rt.LookUp)
.WithMany(lk => lk.RequestTimes)
.HasForeignKey(rt => rt.LookUpTypId);
this.HasRequired<Term>(rt => rt.Term)
.WithMany(t => t.RequestTimes)
.HasForeignKey(rt => rt.TermId);
Thank you
As I understand, the ForeignKey attribute should be used in the property that represents your foreign key.
If you have a object as a property, it should be right above it... unless you also have a separate property for it's Id. In that case, you should use the attribute above the Id property.
[ForeignKey("LookUpTypId")]
public virtual int LookUpTypId { get; set; }
[ForeignKey("TermId")]
public virtual decimal TermId { get; set; }
public virtual Term Term { get; set; }
public virtual LookUp LookUp { get; set; }
And... is this ID decimal?

Entity Framework Code First Fluent API One to Many

I have a ProductRequests table. It has a one to one relationship to ProductRequestDepartments. Which works correctly. I want to link ProductRequestDetails (which will have the actual Products (1 or more) of the ProductRequests.
public partial class WP__ProductRequests
{
[Key]
public int RequestId { get; set; }
[Required]
[StringLength(30)]
public string FromLocation { get; set; }
[Required]
public int ToDepartmentId { get; set; }
[StringLength(4000)]
public string Reason { get; set; }
[Required]
[StringLength(50)]
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
[StringLength(50)]
public string CompletedBy { get; set; }
public DateTime? CompletedDate { get; set; }
[Required]
[StringLength(1)]
public string Status { get; set; }
public ICollection<WP__ProductRequestDetails> ProductRequestDetails { get; set; }
public ICollection<WP__ProductRequestDepartments> ProductRequestDepartments { get; set; }
}
public partial class WP__ProductRequestDetails
{
[Key]
public int RequestDetailsId { get; set; }
[Required]
public int RequestId { get; set; }
[StringLength(20)]
public string ItemCode { get; set; }
[StringLength(100)]
public string ItemName { get; set; }
public int? Quantity { get; set; }
[Required]
[StringLength(1)]
public string Approved { get; set; }
public WP__ProductRequests ProductRequest { get; set; }
}
public partial class WP_ProductRequestDepartments
{
[Key]
public int ID { get; set; }
[StringLength(100)]
public string Department { get; set; }
public int? ApprovalManager { get; set; }
[StringLength(60)]
public string Reason { get; set; }
[StringLength(25)]
public string GeneralLedger { get; set; }
}
How do I wire this up in the Fluent API. So far I tried
public virtual DbSet<WP__ProductRequestDepartments> WP__ProductRequestDepartments { get; set; }
public virtual DbSet<WP__ProductRequestDetails> WP__ProductRequestDetails { get; set; }
public virtual DbSet<WP__ProductRequests> WP__ProductRequests { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<WP__ProductRequestDetails>()
.Property(e => e.Approved)
.IsFixedLength()
.IsUnicode(false);
modelBuilder.Entity<WP__ProductRequests>()
.Property(e => e.Status)
.IsFixedLength()
.IsUnicode(false);
modelBuilder.Entity<WP__ProductRequests>()
.HasRequired(a => a.ProductRequestDepartments)
.WithMany()
.HasForeignKey(a => a.ToDepartmentId);
//??
modelBuilder.Entity<WP__ProductRequests>()
.HasRequired(a => a.ProductRequestDetails)
.WithMany()
.HasForeignKey(a => a.RequestId);
}
ProductRequests -> ProductRequestDepartments works correctly (1 : 1)
ProductRequests -> ProductRequestDetail does NOT work (1 : N)
I'm getting
One or more validation errors were detected during model generation:"
WP__ProductRequests_ProductRequestDetails_Source: : Multiplicity is not valid in Role 'WP__ProductRequests_ProductRequestDetails_Source' in relationship 'WP__ProductRequests_ProductRequestDetails'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.
I believe you are looking for
modelBuilder.Entity<WP__ProductRequestDetails>()
.HasRequired(productRequestDetails => productRequestDetails.ProductRequest)
.WithMany(productRequest => productRequest.ProductRequestDetails)
.HasForeignKey(productRequestDetails => productRequestDetails.RequestId);

Omu.ValueInjecter Mapping with Navigation Properties in EF

According to Valueinjecter mapping with source and Target are usually done with naming convention. But it doesn't work in my case , How could i manage mapping of navigation properties.
DTO
public class EmployeeDTO
{
public long EmployeeId { get; set; }
public long? LoginId { get; set; }
public string EmpNumber { get; set; }
public string FirstName { get; set; }
public string CompanyEmail { get; set; }
public string PersonalEmail { get; set; }
public AttendanceTimeSlotDTO AttendanceTimeSlot { get; set; }
}
public class AttendanceTimeSlotDTO
{
public int SlotId { get; set; }
public TimeSpan InTime { get; set; }
public TimeSpan OutTime { get; set; }
}
MYData Provider
public List<EmployeeDTO> GetActiveEmployees()
{
var employees = UnitOfWork.EmployeeRepository.Get(employee => employee.IsActive, null, "AttendanceTimeSlot").ToList();
//This work fine
var employeesDto = employees.Select(x => new EmployeeDTO().InjectFrom(x)).Cast<EmployeeDTO>().ToList();
employeesDto.InjectFrom(employees);
// Not Working
var result =employees.Select(e => new AttendanceTimeSlot().InjectFrom(e)).Cast<AttendanceTimeSlot>()
.Select(x => new EmployeeDTO().InjectFrom(x)).Cast<EmployeeDTO>().ToList();
}
MYEF
public long EmployeeId { get; set; }
public Nullable<long> LoginId { get; set; }
public string EmpNumber { get; set; }
public string FirstName { get; set; }
public virtual ICollection<Attendance> Attendances { get; set; }
public virtual ICollection<PermanentAddress> PermanentAddresses { get; set; }
public virtual ICollection<TemporaryAddress> TemporaryAddresses { get; set; }
public virtual AttendanceTimeSlot AttendanceTimeSlot { get; set; }
How would i map Navigation Properties with OMU.ValueInjector
by default ValueInjecter maps properties with same name and type
the line
employeesDto.InjectFrom(employees);
is not needed, because it doesn't do anything
here:
employees.Select(e => new AttendanceTimeSlot().InjectFrom(e))
I don't see any matching properties between AttendanceTimeSlot and your MYEF
so MYEF doesn't has int SlotId, TimeSpan InTime or TimeSpan OutTime, so the above line return a collection of newly created untouched AttendanceTimeSlot
for an example of using ValueInjecter with EntityFramework (code first) have a look at this demo project: http://prodinner.codeplex.com

Entity Framework DELETE statement conflicted with the REFERENCE constraint

I’m pretty new to EF and I have a little problem.
I just want to delete an item in my database. I’m using SQL Server 2012 Express, VS2012, AdventureWorks 2012.
The query that I execute is the following:
context = new AWEntities();
var removedItem = context.Addresses
.Include("StateProvince")
.Include("SalesOrderHeaders")
.Include("BusinessEntityAddresses").Single(d => d.AddressID == 11);
context.Addresses.Remove(removedItem);
context.SaveChanges();
The error that I get is
The DELETE statement conflicted with the REFERENCE constraint "FK_SalesOrderHeader_Address_ShipToAddressID". The conflict occurred in database "AdventureWorks2012", table "Sales.SalesOrderHeader", column 'ShipToAddressID'.
The statement has been terminated.
Is this actually a good way to delete items and the according entries in the other tables?
Please point me into the right direction.
public partial class Address
{
public Address()
{
this.BusinessEntityAddresses = new HashSet<BusinessEntityAddress>();
this.SalesOrderHeaders = new HashSet<SalesOrderHeader>();
}
public int AddressID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public int StateProvinceID { get; set; }
public string PostalCode { get; set; }
public System.Data.Spatial.DbGeography SpatialLocation { get; set; }
public System.Guid rowguid { get; set; }
public System.DateTime ModifiedDate { get; set; }
public virtual StateProvince StateProvince { get; set; }
public virtual ICollection<BusinessEntityAddress> BusinessEntityAddresses { get; set; }
public virtual ICollection<SalesOrderHeader> SalesOrderHeaders { get; set; }
}
public partial class StateProvince
{
public StateProvince()
{
this.Addresses = new HashSet<Address>();
this.SalesTaxRates = new HashSet<SalesTaxRate>();
}
public int StateProvinceID { get; set; }
public string StateProvinceCode { get; set; }
public string CountryRegionCode { get; set; }
public bool IsOnlyStateProvinceFlag { get; set; }
public string Name { get; set; }
public int TerritoryID { get; set; }
public System.Guid rowguid { get; set; }
public System.DateTime ModifiedDate { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual CountryRegion CountryRegion { get; set; }
public virtual ICollection<SalesTaxRate> SalesTaxRates { get; set; }
public virtual SalesTerritory SalesTerritory { get; set; }
}
}
public partial class BusinessEntityAddress
{
public int BusinessEntityID { get; set; }
public int AddressID { get; set; }
public int AddressTypeID { get; set; }
public System.Guid rowguid { get; set; }
public System.DateTime ModifiedDate { get; set; }
public virtual Address Address { get; set; }
public virtual AddressType AddressType { get; set; }
public virtual BusinessEntity BusinessEntity { get; set; }
}
public partial class SalesOrderHeader
{
public SalesOrderHeader()
{
this.SalesOrderDetails = new HashSet<SalesOrderDetail>();
this.SalesOrderHeaderSalesReasons = new HashSet<SalesOrderHeaderSalesReason>();
}
public int SalesOrderID { get; set; }
public byte RevisionNumber { get; set; }
public System.DateTime OrderDate { get; set; }
public System.DateTime DueDate { get; set; }
public Nullable<System.DateTime> ShipDate { get; set; }
public byte Status { get; set; }
public bool OnlineOrderFlag { get; set; }
public string SalesOrderNumber { get; set; }
public string PurchaseOrderNumber { get; set; }
public string AccountNumber { get; set; }
public int CustomerID { get; set; }
public Nullable<int> SalesPersonID { get; set; }
public Nullable<int> TerritoryID { get; set; }
public int BillToAddressID { get; set; }
public int ShipToAddressID { get; set; }
public int ShipMethodID { get; set; }
public Nullable<int> CreditCardID { get; set; }
public string CreditCardApprovalCode { get; set; }
public Nullable<int> CurrencyRateID { get; set; }
public decimal SubTotal { get; set; }
public decimal TaxAmt { get; set; }
public decimal Freight { get; set; }
public decimal TotalDue { get; set; }
public string Comment { get; set; }
public System.Guid rowguid { get; set; }
public System.DateTime ModifiedDate { get; set; }
public virtual Address Address { get; set; }
public virtual ShipMethod ShipMethod { get; set; }
public virtual CreditCard CreditCard { get; set; }
public virtual CurrencyRate CurrencyRate { get; set; }
public virtual Customer Customer { get; set; }
public virtual ICollection<SalesOrderDetail> SalesOrderDetails { get; set; }
public virtual SalesPerson SalesPerson { get; set; }
public virtual SalesTerritory SalesTerritory { get; set; }
public virtual ICollection<SalesOrderHeaderSalesReason> SalesOrderHeaderSalesReasons { get; set; }
}
Can't really tell much from what you have said, but you may benefit from looking into using the DbModelBuilder to solve cascade issues:
modelBuilder.Entity<Parent>()
.HasMany<Child>(c => c.Children)
.WithOptional(x => x.Parent)
.WillCascadeOnDelete(true);
Again, would need more information about your model structure to determine if this is the right approach.
Either that or in your delete method, remove any children first, and then remove the parent.
modelBuilder.Entity<Parent>()
.HasMany<Child>(c => c.Children)
.WithOptional(x => x.Parent)
.WillCascadeOnDelete(true);
or use Include
var adv = db.Adv.Include(b => b.Features)
.Include(b => b.AdvDetails)
.Include(b => b.AdvGallery)
.FirstOrDefault(b => b.Id == id);
db.Adv.Remove(adv);
for .HasMany(...).WithMany(...) Include is ok
You can resolve this issue on SQL side
Method 1 :
First, you need to find on which table this FK constraint has been defined, through using Replication monitor.
Right click on that FK, click Modify, you should get popup box like one shown below.
From the popup box, Select Cascade for del.
Method 2 :
set ON DELETE CASCADE in sql at the end of constraint.
In EF Core the syntax in builder is as follows:
builder.HasOne(b => b.Parent )
.WithMany(a => a.Children)
.OnDelete(DeleteBehavior.Cascade);
https://learn.microsoft.com/en-us/ef/core/saving/cascade-delete
I got this error when I created Entity B, that referenced Entity A, and then tried to delete Entity A. SQL/EF did not allow me to leave that dangling Id reference, since the objcet no longer existed. Cascading deletes would solve this, but I wanted B to persist. So I have to remove the reference from B before deleting A:
var existingContractApprovers = _repo.Query<ChangeOrderApproverForContract>().Where(coafc => coafc.ContractId == key).ToList();
//remove refs to contract approvers to preserve data integrity
foreach(var contractApp in existingContractApprovers)
{
var associatedChangeOrderApprovers = _repo.Query<ChangeOrderApprover>().AsNoTracking().Where(coafc => coafc.ChangeOrderApproverForContractId == contractApp.Id).ToList();
foreach(var coApp in associatedChangeOrderApprovers)
{
_repo.Edit(coApp);
coApp.ChangeOrderApproverForContractId = null;
}
}
_repo.SaveChanges();
//remove the contract approvers
foreach (var contractApp in existingContractApprovers)
{
_repo.Delete(contractApp);
}
_repo.SaveChanges();
You can do this in EF Core.
modelBuilder.Entity<Parent>()
.HasMany<Child>(c => c.Children)
.WithOne(s => s.Parent)
.OnDelete(DeleteBehavior.Cascade);
The safer alternative is to make sure all children are deleted before deleting the parent. You should cascade on delete only if you are completely aware of how your entities relate. For example, you could have lots of orders connected to
a certain category in your e-commerce store. Once the category is deleted, all the orders and any entity whose foreign keys are connected to this parent category will be gone.