Entity Framework suggest invalid field name - c#

I have two tables in my DataBase, BUNTS, which contains information about pieces of steel
CREATE TABLE BUNTS (
BUNTCODE INTEGER NOT NULL,
BUNTNAME VARCHAR(20),
BUNTSTEEL INTEGER,
......
);
and POLL_WEIGHT_BUNTS, which contains information about operations that had been performed on each bunt
CREATE TABLE POLL_WEIGHT_BUNTS (
PWBCODE INTEGER NOT NULL,
PWBBUNTCODE INTEGER,
PWBDEPARTMENTFROM INTEGER,
PWBDEPARTMENTTO INTEGER
....
);
The relationship is one-to-many. I mapped those tables to models. Everything worked just fine.
Recently I've decided to add a field to table BUNTS which would reference to the last operation that had been performed on bunt:
BUNTLASTOPER INTEGER
Now my models look like this:
[Table("BUNTS")]
public class Bunt
{
[Key]
[Column("BUNTCODE")]
public int? Code { set; get; }
[Column("BUNTNAME")]
public string Name { set; get; }
[Column("BUNTSTEEL")]
public int? SteelCode { set; get; }
[Column("BUNTLASTOPER")]
public int? LastOperationID { set; get; }
[ForeignKey("LastOperationID")]
public BuntOperation LastOperation { set; get; }
public virtual ICollection<BuntOperation> Operations { set; get; }
}
[Table("POLL_WEIGHT_BUNTS")]
public class BuntOperation
{
[Key]
[Column("PWBCODE")]
public int? Code { set; get; }
[Column("PWBBUNTCODE")]
public int? BuntCode { set; get; }
[ForeignKey("BuntCode")]
public Bunt Bunt { set; get; }
[Column("PWBDEPARTMENTFROM")]
public int? DepartmentFromCode { set; get; }
.....
}
After I've made this, when I try to query Operations like this
return _context.Operations;
it generates an SQL-statement with new incorrect field Bunt_Code
SELECT
"B"."PWBCODE" AS "PWBCODE",
"B"."PWBBUNTCODE" AS "PWBBUNTCODE",
"B"."PWBDEPARTMENTFROM" AS "PWBDEPARTMENTFROM",
....
"B"."Bunt_Code" AS "Bunt_Code"
FROM "POLL_WEIGHT_BUNTS" AS "B"
I assume that now EF looks for a field that is a foreign key for BUNTS table, and cant find it. So it generates Bunt_Code field, which is missing in my database. But I already have a property Bunt in BuntOperation class, which references to BUNTS table. What am I missing?
UPDATE
seems like this solves my problem
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Bunt>().HasOptional(b => b.LastOperation).WithMany();
modelBuilder.Entity<Bunt>().HasMany(b => b.Operations).WithRequired(op => op.Bunt);
}

seems like this solves my problem
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Bunt>().HasOptional(b => b.LastOperation).WithMany();
modelBuilder.Entity<Bunt>().HasMany(b => b.Operations).WithRequired(op => op.Bunt);
}

Related

c# EntityFramework CodeFirst migration creates a new column for existing one

in my Database models i added a second Column to a table which already had one link to the same table before.
My Model before my changes looks as followed:
[Table("RepairProcesses", Schema = "Data")]
public class RepairProcess : DatabaseBase
{
[DataMember]
[Column("SerialNumberID")]
public Guid SerialNumberID { get; set; }
[ForeignKey("SerialNumberID")]
public virtual SerialNumber SerialNumber { get; set; }
}
Now i added the second Link to the Table and the Model now looks as followed:
[Table("RepairProcesses", Schema = "Data")]
public class RepairProcess : DatabaseBase
{
[DataMember]
[Column("SerialNumberID")]
public Guid SerialNumberID { get; set; }
[DataMember]
[Column("ReplacementSerialNumberID")]
public Guid? ReplacementSerialNumberID { get; set; }
[ForeignKey("SerialNumberID")]
public virtual SerialNumber SerialNumber { get; set; }
[ForeignKey("ReplacementSerialNumberID")]
public virtual SerialNumber ReplacementSerialNumber { get; set; }
}
When i now try to create the migrationscript the EntityFramework deletes the foreingn keys and creates a new column with the name SerialNumber_ID.
public override void Up()
{
DropForeignKey("Data.RepairProcesses", "SerialNumberID", "Data.SerialNumbers");
AddColumn("Data.RepairProcesses", "SerialNumber_ID", c => c.Guid());
AddColumn("Data.RepairProcesses", "ReplacementSerialNumberID", c => c.Guid());
CreateIndex("Data.RepairProcesses", "SerialNumber_ID");
CreateIndex("Data.RepairProcesses", "ReplacementSerialNumberID");
AddForeignKey("Data.RepairProcesses", "ReplacementSerialNumberID", "Data.SerialNumbers", "ID");
AddForeignKey("Data.RepairProcesses", "SerialNumber_ID", "Data.SerialNumbers", "ID");
}
public override void Down()
{
DropForeignKey("Data.RepairProcesses", "SerialNumber_ID", "Data.SerialNumbers");
DropForeignKey("Data.RepairProcesses", "ReplacementSerialNumberID", "Data.SerialNumbers");
DropIndex("Data.RepairProcesses", new[] { "ReplacementSerialNumberID" });
DropIndex("Data.RepairProcesses", new[] { "SerialNumber_ID" });
DropColumn("Data.RepairProcesses", "ReplacementSerialNumberID");
DropColumn("Data.RepairProcesses", "SerialNumber_ID");
AddForeignKey("Data.RepairProcesses", "SerialNumberID", "Data.SerialNumbers", "ID");
}
The problem is that this is a productive database and when the EF creates a new Field for the SerialNumber i lost the links to the correct field.
When i only delete the lines in the Up/Down Script which creates the new column i ran into problems because the EF Expects a Column named SerialNumber_ID.
Thanks for your help,
Michael
Try to change SerialNumber class this way:
public class SerialNumber
{
//other stuff....
[InverseProperty("SerialNumber")]
public virtual ICollection<RepairProcess> SerialNumbers {get;set;}
[InverseProperty("ReplacementSerialNumber")]
public virtual ICollection<RepairProcess> ReplacementSerialNumbers {get;set;}
}

The value of the navigation property of my EntityFramework class created in code fist mode always be null

I am a beginner of EntityFramework. The codes below is extracted form my project.
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int UserId { get; set; }
public virtual int UserType { get; set; }
}
public class Person : User
{
public override int UserType
{
get
{
return 0;
}
set
{
base.UserType = 0;
}
}
public string PersonName { get; set; }
public ICollection<Sunny.Models.WorkExperience> WorkExperiences { get; set; }
}
public class WorkExperience
{
[Key]
public int ExperienceId { get; set; }
public int PersonId { get; set; }
public string Job { get; set; }
[ForeignKey("PersonId")]
public Person Person { get; set; }
}
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
this.Map<User>(user => user.ToTable("User"));
this.Map<Person>(person => person.ToTable("Person"));
}
}
public class DbContext : System.Data.Entity.DbContext
{
public DbContext() : base("name=Model")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserConfiguration());
modelBuilder.Conventions.Remove<Conventions.PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
public DbSet<User> Users { get; set; }
public DbSet<Person> Persons { get; set; }
public DbSet<WorkExperience> WorkExperiences { get; set; }
}
static void Main(string[] args)
{
DbContext context = new Models.DbContext();
Person person = new Models.Person();
person.UserId = 1;
person.PersonName = "Name";
context.Persons.Add(person);
WorkExperience experience = new Models.WorkExperience();
experience.PersonId = 1;
experience.Job = "Coder";
context.WorkExperiences.Add(experience);
context.SaveChanges();
context = new DbContext();
Console.Write(context.WorkExperiences.First().Person == null);
Console.Read();
}
The running result of the Main method above is displaying true ,That is to say ,the value of the property WorkExperiences.Person always be null .But i have inserted data into the tables .
How to let the property WorkExperiences.Person load with the referenced key value ? Thanks in advance for any help.
Entity framework won't automatically load associated entities unless you specifically query for them.The reason is that it would be too easy to load far more than you expected if you always loaded all navigation properties - you might end up pulling most of your database back even on a simple query, if you have a lot of relationships. Imagine if you went to Amazon and it ran a query for your orders, which then included all products in those orders, which then included all sellers from those products, which then included all products from those sellers, ...
Entity Framework gives you several techniques to control when you want to load related data.
You can use DbExtensions.Include() to force it to include a related entity with the original query, which means one trip to the database:
Console.Write(context.WorkExperiences.Include(w => w.Person).First().Person == null);
Alternatively, you can use .Load() to force the load of an entity which isn't loaded:
var firstWE = context.WorkExperiences.First();
firstWE.Reference("Person").Load();
Console.Write(firstWE.Person == null);
Or you can enable lazy loading, which will make it load on demand the first time you access the property. You do this by adding virtual to it (which allows EF the ability to add some code to your property and load on demand):
public virtual Person Person { get; set; }

Entity Framework one-to-many and many-to-many relationship

im currently learning the entity framework and been miserably failing with this task for a couple of days... I would like to achieve the following Database Scheme:
TableA:
ClassA_ID
ClassB_ID(foreign key - one to one)
TableATableB:(one-to-many relationship)
ClassA_ID
ClassB_ID
TableB:
ClassA_ID(foreign key - one to one)
ClassB_ID
So I Actually want a specific one to one relationship from Class A to Class B and a one to many Relationship from ClassA to ClassB.
My C# Code Looks like this:
Class A:
public static int idcounter;
[Key]
public int id { get; set; }
public virtual List<ClassB> allClassB { get; set; }
public virtual ClassB currentClassB { get; set; }
public int? currentClassBID { get; set; }
public ClassA(){
idcounter++;
id = idcounter;
allClassB = new List<ClassB>();
currentClassB = new ClassB();
currentClassBID = currentClassB.id;
MyContext.add(this);
}
Class B:
public static int idcounter;
[Key]
public int id { get; set; }
public virtual ClassA owner { get; set; }
public int? ownerID { get; set; }
public ClassB(ClassA a){
idcounter++;
id = idcounter;
owner = a;
ownerID = a.id;
}
MyContext:
public static void add(ClassA a)
{
using (MyContext context = new MyContext ())
{
context.setB.Add(a.currentClassB);
context.setA.Add(a);
context.SaveChanges();
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ClassA>().HasOptional(x => x.currentClassB ).WithMany().HasForeignKey(x => x.currentClassBID ).WillCascadeOnDelete(false);
modelBuilder.Entity<ClassB>().HasOptional(x => x.ClassA).WithMany(x => x.allClassB).HasForeignKey(x => x.ownerID).WillCascadeOnDelete(false) ;
}
Its pretty similar to this question
Entity Framework Code First: How can I create a One-to-Many AND a One-to-One relationship between two tables?
but I just get a Circular Exception when saving.
Unable to determine a valid ordering for dependent operations. Dependencies may
exist due to foreign key constraints, model requirements, or store-generated
values.
Its pretty weird that after
context.setA.Add(a);
it adds currentClassB into allClassB without me ever mention that... it Even suddenly does that when i dont
allClassB = new List<ClassB>();
What am I doing wrong?
You have to use InverseProperty as shown below.
[InverseProperty("allClassB")]
public virtual ClassB currentClassB { get; set; }
public int? currentClassBID { get; set; }
DataAnnotations - InverseProperty Attribute:

Entity splitting with EF Code First issue

So I am trying to achieve entity splitting in EF 6.1 with Code First, and I am running into an error.
I have the following tables:
CREATE TABLE [dbo].[Organization]
(
[OrganizationId] INT NOT NULL PRIMARY KEY IDENTITY,
[TenantId] INT NOT NULL,
[Name] NVARCHAR(80) NOT NULL
)
CREATE TABLE [dbo].[OrganizationSettings]
(
[OrganizationSettingsId] INT NOT NULL PRIMARY KEY IDENTITY,
[OrganizationId] INT NOT NULL,
[AllowMultipleTimers] BIT NOT NULL,
CONSTRAINT [FK_OrganizationSettings_Organization] FOREIGN KEY (OrganizationId) REFERENCES Organization(OrganizationId)
)
With the following model objects:
public partial class Organization
{
public int OrganizationId { get; set; }
public int TenantId { get; set; }
public string Name { get; set; }
public OrganizationSettings Settings { get; set; }
}
public class OrganizationSettings
{
public int OrganizationSettingsId { get; set; }
public int OrganizationId { get; set; }
public bool AllowMultipleTimers { get; set; }
}
With the following config code:
var org = modelBuilder.Entity<Organization>();
org.Map(u =>
{
u.Properties(m => new { m.TenantId, m.Name });
})
.ToTable("Organization");
org.Map(u =>
{
u.Property(m => m.Settings.AllowMultipleTimers).HasColumnName("AllowMultipleTimers");
u.ToTable("OrganizationSettings");
});
Then just the following query:
context.Organizations.FirstOrDefault();
Which yields the following error:
The property 'Settings.AllowMultipleTimers' on type 'Organization'
cannot be mapped because it has been explicitly excluded from the
model or it is of a type not supported by the DbModelBuilderVersion
being used.
What am I doing wrong here?
Update: I forgot to mention that I created the database by hand, and am using the CF fluent API to map my models, rather than using "real" Code First.
While I was pretty sure I had this mapping working before, I went ahead and went a little different route.
First I got rid of the surrogate key on `OrganizationSettings (probably not strictly necessary), and then mapped it as an entity with a 1:1 relationship.
My OrganizationSettings is now:
public class OrganizationSettings
{
public int OrganizationId { get; set; }
public bool AllowMultipleTimers { get; set; }
}
OrganizationId is both a primary key and a foreign key.
And the config is:
var org = modelBuilder.Entity<Organization>()
.Map(u =>
{
u.Properties(m => new { m.TenantId, m.Name });
})
.HasRequired(m => m.Settings)
.WithRequiredPrincipal();
modelBuilder.Entity<OrganizationSettings>()
.HasKey(m => m.OrganizationId);
And this seems to work just fine. Since I'm not exposing a DbSet for OrganizationSettings it keeps the conceptual modeling of OrganizationSettings as a value object intact.
Were you trying to set up OrganizationSettings as a complex type while using entity splitting as well? Something like this, perhaps:
public partial class Organization
{
public int OrganizationId { get; set; }
public int TenantId { get; set; }
public string Name { get; set; }
public OrganizationSettings Settings { get; set; }
}
public class OrganizationSettings
{
public bool AllowMultipleTimers { get; set; }
}
// if you don't have a key defined on OrganizationSettings, this might not be needed
modelBuilder.ComplexType<OrganizationSettings>();
modelBuilder.Entity<Organization>()
.Map(u =>
{
u.Properties(m => new { m.OrganizationId, m.TenantId, m.Name });
u.ToTable("Organization");
})
.Map(u =>
{
u.Properties(m => new { m.OrganizationId, m.Settings.AllowMultipleTimers });
u.ToTable("OrganizationSettings");
// If you wanted to set the key column name
u.Property(m => m.OrganizationId).HasColumnName("OrganizationSettingsId");
});

Lazy Loading EntityFramework 5 (with CodeFirst)

I am testing lazy loading in a C# Console Application. For some reason Lazy loading is not working.
I have checked the LazyLoadingEnabled and ProxyCreationEnabled properties of the context.Configuration. They are true.
My property is virtual.
I have checked the other similar SO questions without success. I am not sure what might be happening.
This is my code (simplified to not show namespaces):
static void Main(string[] args) {
Models.DataContext dc = new Models.DataContext();
Console.WriteLine("Context Lazy {0}. Proxy Creation {1} ",
dc.Configuration.LazyLoadingEnabled,
dc.Configuration.ProxyCreationEnabled);
var grp = dc.Groups.FirstOrDefault();
Console.WriteLine("GroupId {1}, AttrSet is null = {0}",
grp.AttrSet == null , grp.Id);
var grp2 = dc.Groups.Include("AttrSet").FirstOrDefault();
Console.WriteLine("GroupId {1}, AttrSet is null = {0}",
grp2.AttrSet == null, grp2.Id);
}
class Group {
public System.Guid Id { get; set; }
public string Name { get; set; }
public virtual AttrSet AttrSet { get; set; }
}
class AttrSet {
public System.Guid Id { get; set; }
public string Name { get; set; }
}
The output of running this is:
Context Lazy True. Proxy Creation True
GroupId 186ebc8a-dec7-4302-9f84-5a575577baac, AttrSet is null = True
GroupId 186ebc8a-dec7-4302-9f84-5a575577baac, AttrSet is null = False
I am sure that the loaded record is correct and it has a proper AttrSet in the database.
Any ideas?
Update
I created a very simple testing project in case any one actually wants to look at the code.
See: https://bitbucket.org/josea/eflazy (GIT: https://josea#bitbucket.org/josea/eflazy.git).
Proxy generation is not occurring. Why?? Because your POCOs are PRIVATE!! EF can't see them to derive proxies from them. Make your POCOs public and it'll work the way you expect.
public class Group
Are you using anything to configure the 1:1 relationship between the 2 classes? Because it doesn't look like you are here, which would cause Entity Framework to not be able to load the relationship.
You can use Data Annotations to define the FK relationship as so:
public class AttrSet {
public System.Guid Id { get; set; }
public string Name { get; set; }
[Required, ForeignKey("MyGroup")]
public int GroupID { get; set; }
public virtual Group MyGroup { get; set; }
}
This should give you the relationship you wanted. AttrSetId is whatever you've named the FK column in your table, so change that if it is different.
public class Group {
public System.Guid Id { get; set; }
public string Name { get; set; }
public System.Guid AttrSetId {get;set;}
[ForeignKey("AttrSetId")]
public virtual AttrSet AttrSet { get; set; }
}
Edit:
Add this line to your AttrSet class:
public virtual ICollection<Group> Groups {get;set;}
Add this next line to your OnModelCreating in your Models.DataContext. If for some reason you don't already have that function overridden, it'll look like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Group>().HasOptional(x => x.AttrSet).WithMany(x => x.Groups);
}
I put HasOptional instead of HasRequired as I assumed you could save a Group without an AttrSet. If that is not true, and the FK is not nullable, then you should use HasRequired.

Categories

Resources