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

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:

Related

Entity Framework database first many-to-many when the foreign key is different than the primary key

I know the title is a little confusing so let me layout the tables to show the problem I am having.
public partial class ClassA
{
public ClassA()
{
ClassBs = new HashSet<ClassB>();
}
[Key]
public int ClassAID { get; set; }
public int SharedID { get; set; }
//[InverseProperty(nameof(SharedID))]
[ForeignKey(nameof(SharedID))]
public virtual ICollection<ClassB> ClassBs { get; set; }
}
Before I continue, ClassA does not have a direct link to ClassB BUT both ClassA and ClassB have a SharedID and as such, in SQL I can join the two table through the SharedID.
public partial class ClassB
{
[Key]
public int ClassBID { get; set; }
public int SharedID { get; set; }
}
Just to be clear, I get no errors but the SQL join is invalid when looking at it with SQL Server Profiler.
In SQL the join turns into this.
WHERE [ClassA].[ClassAID] = [ClassB].[SharedID]
This is invalid since they both need to join on SharedID.
Any ideas how to fix this?
I have tried InverseProperty, ForeignKey, HasMany, WithMany, MapLeftKey / MapRightKey, HasForeignKey.
Each of those seem to have their own set of errors.
Not sure what else to try.
Here is the HasForeignKey approach I tried:
modelBuilder.Entity<ClassA>()
.HasMany(x => x.ClassBs)
.WithOptional()
.HasForeignKey(x => x.SharedID);
Here is the MapKey attempt:
modelBuilder.Entity<ClassA>()
.HasMany(x => x.ClassBs)
.WithOptional().Map(x => { x.MapKey("SharedID"); });

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 suggest invalid field name

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);
}

Entity framework relationships

I have these three entities:
public class Dog
{
public int DogId { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public bool Checked { get; set; }
public string DogImage { get; set; }
public virtual ICollection<Result> Results { get; set; }
}
public class Event
{
public int EventId { get; set; }
public string EventName { get; set; }
public string EventLocation { get; set; }
public string EventType { get; set; }
public string EventDate { get; set; }
public virtual ICollection<Result> Results { get; set; }
}
public class Result
{
public int ResultId { get; set; }
public int Track { get; set; }
public int Obedience { get; set; }
public int Protection { get; set; }
[ForeignKey("Dog")]
public int DogId { get; set; }
public virtual Dog Dog { get; set; }
[ForeignKey("Event")]
public int EventId { get; set; }
public virtual Event Event { get; set; }
}
I´ve been getting help from here before in order to set it up like this.
Entity Framework errors when trying to create many-to-many relationship
So the way it is now I guess the result is the "glue" that ties these classes together containing foreign keys to the two other tables.
What I have been trying to achieve for days now is to:
Create an event.
Add dogs to the event.
Add results to the dogs participating in the choosenEvent.
Lets say I create an event like this:
[HttpPost]
public ActionResult CreateEvent(Event newEvent)
{
newEvent.EventDate = newEvent.EventDate.ToString();
_ef.AddEvent(newEvent);
return View();
}
Now I guess the next step would be to add a list of dogs to this event and in order to do that I need to somehow use my result-class since that's the "glue"-class. Please let me know if I'm even on the right track here.
It is not really a good idea to do many to many relationships like how you've done. See here
In order to get a proper many to many relationship, mapped in the proper way in the database, that doesn't have pitfalls, I would try it this way:
public class Dog {}
public class Event {}
public class Result {}
// This is a linking table between Dog and Results
public class DogResult
{
public int Id {get;set;}
public int DogId {get;set;}
public int ResultId {get;set;}
}
// This is a linking table between Events and Results
public class EventResult
{
public int Id {get;set;}
public int EventId {get;set;}
public int ResultId {get;set;}
}
When you now write your query you can do this:
using (var context = new DbContext())
{
var dogs = context.Dogs();
var dogResults = context.DogResults();
var results = context.Results();
var dogsAndResults = dogs.Join(
dogResults,
d => d.Id,
r => r.DogId,
(dog, dogResult) => new { dog, dogResult })
.Join(
results,
a => a.dogResult.ResultId,
r => r.Id,
(anon, result) => new { anon.dog, result });
}
It is a bit nasty looking, but it will give you back a list of anonymous objects containing a Dog and its related Result. But obviously it would be better to do this in a stored proc:
using (var context = new DbContext())
{
var results = context.Database.ExecuteStoreQuery<SomeResultDto>("SELECT * .... JOIN ... ");
}
This is cleaner, because you are using SQL.
This is a more complex way of dealing with it. But far more performant, especially if you understand fully how entity framework executes LINQ.
Obviously if you want to create these links:
using (var context = new DbContext())
{
context.Dogs.AddRange(dogs); // dogs being a list of dog entities
context.Results.AddRange(results); // events being a list of results entities
context.DogResults.AddRange(dogResults); // a list of the links
}
It is completely up to you how you create these links. To turn this into a sproc as well, you want to create some custom User Defined Table Types and use them as a Table Value Parameter.
var dogResults = dogs.SelectMany( d => results.Select ( r => new DogResult { DogId = d.Id, ResultId = r.Id } ) );
That is a beast of a LINQ query and basically it gets every dog and links it to every result. Run it in LinqPad and Dump the values.
I've only done this using the fluent method (when I was learning I found you can do everything in fluent, but not with annotations, so I've not looked into them), the following creates a many to many between my Unit entity and my UnitService entity:
modelBuilder.Entity<Unit>()
.HasMany<UnitService>(u => u.Services)
.WithMany(us => us.Units);
This code is in the protected override void OnModelCreating(DbModelBuilder modelBuilder) method.
In your case Event is Unit and Dog is UnitService.
Oh ooops, you don't need that at all, your 'join' table is your results table, in my case I don't care about the join table so its all hidden.
Maybe something like:
modelBuilder.Entity<Result>()
.HasMany<Event>(e => e.Results);
modelBuilder.Entity<Result>()
.HasMany<Dog>(d => d.Results);

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