Should DbSets of nested classes be defined in DbContext? - entity-framework

Consider the following code:
public class MainObject
{
public int Id { get; set; }
List<NestedObject> NestedObjects { get; set; }
}
public class NestedObject
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ExampleContext : DbContext
{
DbSet<MainObject> MainObjects { get; set; }
// Should it be here? Code works without it...
// DbSet<NestedObject> NestedObjects { get; set; }
}
// ( ... )
var nestedFooObjects = exampleContext.Get<NestedObject>()
.Where(obj => obj.Name == "foo").ToList(); // still works
I was under the impression that a DBContext should contain all tables that are directly accessed from code. So without DbSet< NestedObject > it should only be possible to get MainObjects and then look for NestedObjects within the main ones. That is not the case apparently, as my experiments show. I want to get NestedObjects directly as in the example above. In that case: Should DbSet< NestedObject > be defined at all? Its not necessary for the code to run...

Related

Parent object is null when getting child objects using Entity Framework

public class AcsBatchingDbContext : DbContext
{
public DbSet<CardHolder> CardHolders { get; set; }
public DbSet<AccessCard> AccessCards { get; set; }
}
public class CardHolder
{
public int CardHolderId { get; set; }
public ICollection<AccessCard> AccessCards { get; set; };
}
public class AccessCard
{
public int AccessCardId { get; set; }
public CardHolder CardHolder { get; set; }
}
When I try to get AccessCards
using (var db = new AcsBatchingDbContext())
{
var cards = db.AccessCards.ToList();
}
Where card.CardHolder = null
Why? Why EF doesnt grab the CardHolder?
Another question:
Why this expression doesnt compile?
db.AccessCards.Include(x => x.CardHolder).ToList();
Why the only options is to use is
db.AccessCards.Include("CardHolder").ToList();
You should use include to load CardHolder entity.
(This requires using System.Data.Entity;)
db.AccessCards.Include(x => x.CardHolder).ToList();
Alternatively, you can apply Include like this;
db.AccessCards.Include("CardHolder").ToList();

Entity framework replaces delete+insert with an update. How to turn it off

I want to remove a row in database and insert it again with the same Id, It sounds ridiculous, but here is the scenario:
The domain classes are as follows:
public class SomeClass
{
public int SomeClassId { get; set; }
public string Name { get; set; }
public virtual Behavior Behavior { get; set; }
}
public abstract class Behavior
{
public int BehaviorId { get; set; }
}
public class BehaviorA : Behavior
{
public string BehaviorASpecific { get; set; }
}
public class BehaviorB : Behavior
{
public string BehaviorBSpecific { get; set; }
}
The entity context is
public class TestContext : DbContext
{
public DbSet<SomeClass> SomeClasses { get; set; }
public DbSet<Behavior> Behaviors { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Entity<SomeClass>()
.HasOptional(s => s.Behavior)
.WithRequired()
.WillCascadeOnDelete(true);
}
}
Now this code can be executed to demonstrate the point
(described with comments in the code below)
using(TestContext db = new TestContext())
{
var someClass = new SomeClass() { Name = "A" };
someClass.Behavior = new BehaviorA() { BehaviorASpecific = "Behavior A" };
db.SomeClasses.Add(someClass);
// Here I have two classes with the state of added which make sense
var modifiedEntities = db.ChangeTracker.Entries()
.Where(entity => entity.State != System.Data.Entity.EntityState.Unchanged).ToList();
// They save with no problem
db.SaveChanges();
// Now I want to change the behavior and it causes entity to try to remove the behavior and add it again
someClass.Behavior = new BehaviorB() { BehaviorBSpecific = "Behavior B" };
// Here it can be seen that we have a behavior A with the state of deleted and
// behavior B with the state of added
modifiedEntities = db.ChangeTracker.Entries()
.Where(entity => entity.State != System.Data.Entity.EntityState.Unchanged).ToList();
// But in reality when entity sends the query to the database it replaces the
// remove and insert with an update query (this can be seen in the SQL Profiler)
// which causes the discrimenator to remain the same where it should change.
db.SaveChanges();
}
How to change this entity behavior so that delete and insert happens instead of the update?
A possible solution is to make the changes in 2 different steps: before someClass.Behavior = new BehaviorB() { BehaviorBSpecific = "Behavior B" }; insert
someClass.Behaviour = null;
db.SaveChanges();
The behaviour is related to the database model. BehaviourA and B in EF are related to the same EntityRecordInfo and has the same EntitySet (Behaviors).
You have the same behaviour also if you create 2 different DbSets on the context because the DB model remains the same.
EDIT
Another way to achieve a similar result of 1-1 relationship is using ComplexType. They works also with inheritance.
Here an example
public class TestContext : DbContext
{
public TestContext(DbConnection connection) : base(connection, true) { }
public DbSet<Friend> Friends { get; set; }
public DbSet<LessThanFriend> LessThanFriends { get; set; }
}
public class Friend
{
public Friend()
{Address = new FullAddress();}
public int Id { get; set; }
public string Name { get; set; }
public FullAddress Address { get; set; }
}
public class LessThanFriend
{
public LessThanFriend()
{Address = new CityAddress();}
public int Id { get; set; }
public string Name { get; set; }
public CityAddress Address { get; set; }
}
[ComplexType]
public class CityAddress
{
public string Cap { get; set; }
public string City { get; set; }
}
[ComplexType]
public class FullAddress : CityAddress
{
public string Street { get; set; }
}

Adding a sub collection of new object and my new object into the database with Entity framework

The method is really simple and I don't see what am I missing...
public int SaveEvent(Data.Models.Event evnt)
{
db.Events.Add(evnt);
db.SaveChanges();
return evnt.EventId;
}
here is the object declaration:
public class Event
{
public int EventId { get; set; }
public string Name { get; set; }
public ICollection<EventTag> EventTags { get; set; }
}
The evnt object contains a property name EventTags that contains 6 new elements.
The evnt is inserted in the database but not the EventTag... any idea ? no error nothing. just the EventTag are not added...
public class EventDbContext : DbContext
{
public DbSet<Event> Events { get; set; }
public DbSet<EventTag> EventTags { get; set; }
public DbSet<Tag> Tags { get; set; }
}
Here is a screenshot of the value:
If the EventTags are not being added to the database you may need to manually specify the EntityState for each tag.
public int SaveEvent(Data.Models.Event evnt)
{
foreach(var tag in evnt.EventTags)
{
db.Entry(tag).State = EntityState.Added;
}
db.Events.Add(evnt);
db.SaveChanges();
return evnt.EventId;
}
You might also want to update your class definition and set the EventTags property as virtual.
public class Event
{
public int EventId { get; set; }
public string Name { get; set; }
public virtual ICollection<EventTag> EventTags { get; set; }
}
In your screenshot it looks like the tags are loading, but not the Location property on the tags. If that's the case, then make sure to set the Location property to virtual as well.

Avoiding adding a duplicate

I am learning EF. I have code that looks as below.
I added the Key annotations because when I add a symbol, it should be added once and no more. So if add EUR/USD, I don't want a different EUR/USD. However, on different runs of this program, when I run the code, it complains that the key is already there. How do either created the context if it is not already in the db, or get a reference to it if it already exists?
using (var db = new TickDataTestContext())
{
var td = new SymbolTickDataEntity { Symbol = symbol };
db.SymbolTickData.Add(td);
db.SaveChanges();
while (true)
{
etc....
public class SymbolTickDataEntity
{
public int SymbolTickDataEntityID { get; set; }
[Key]
[Required]
public string Symbol { get; set; }
public virtual IList<MarketDataDepthEntity> Mdds { get; set; }
public SymbolTickDataEntity() { Mdds = new List<MarketDataDepthEntity>(); }
}
public class TickDataTestContext : DbContext
{
public DbSet<MarketDataEntity> MarketData { get; set; }
public DbSet<MarketDataDepthEntity> MarketDataDepth { get; set; }
public DbSet<SymbolTickDataEntity> SymbolTickData { get; set; }
}
You could check SymbolTickData to see if that exists before inserting it.
if(!db.SymbolTickData.Any(a => a.Symbol.Equals(symbol)))
{
db.SymbolTickData.Add(td);
db.SaveChanges();
}

Eager loading including navigational property of derived class

Sample class structure
class Order
{
public int Id { get; set; }
public DateTime Date { get; set; }
public List<OrderDetail> Details { get; set; }
}
class OrderDetail
{
public int Id { get; set; }
public int Qty { get; set; }
public Item Item { get; set; }
}
class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
class ElectronicItem : Item
{
public MoreDetail Detail { get; set; }
}
class MoreDetail
{
public int Id { get; set; }
public string SomeData { get; set; }
}
In order to populate order object with all navigational properties, I wrote
context.Orders.Include("Details").Include("Details.Item")
I also want to load MoreDetail object, hence I tried
context.Orders.Include("Details").Include("Details.Item.Detail")
It didn't work. How to load complete Order object?
It is currently not possible but it is feature requested by community on User DataVoice as you already found. There is also related bug on MS Connect.
You simply cannot eager load navigation properties of derived types but you can load them with separate query:
var moreDetails = context.MoreDetails;
EF should automatically fix your navigation properties. If you use filtering on orders in your original query you must apply that filter in more details query as well:
var moreDetails = cotnext.MoreDetials.Where(m => m.Item.Order ....);