I would like to know if entity framework automatically tracks changes in navigational properties for an entity and also updates those changes when the related entity is updated using SaveChanges(). I am using a generic repository, unit of work, ninject with code first entity framework 5 in MVC 4. I have a post and tags tables that has a many-to-many relationship. I have a join table for this relationship, named PostTagMap, with two columns, Post_id and Tag_id. When I add a new post with its associated tags, the new post is successfully added to the post table and the PostTagMap also saves the tags associated with this post. However, when I try to edit/update the post and its associated tags, only the scalar properties of the post gets updated and not the associated tags. I tried debugging and I find that the post entity to be updated does have the updated values for the tags navigational property but entity framework only generates the update statement for the post table and not for the PostTagMap. What should I do to update the tags navigational property for the post as well.
Following are the classes used
DBContext class
public class EFDbContext : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>()
.HasMany<Tag>(p => p.Tags)
.WithMany(u => u.Posts)
.Map(m =>
{
m.ToTable("PostTagMap");
m.MapLeftKey("Post_id");
m.MapRightKey("Tag_id");
});
}
}
Post class
[Table("Post")]
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
Tag class
[Table("Tag")]
public class Tag
{
public int Id { get; set; }
public string Name { get; set; }
[JsonIgnore]
public ICollection<Post> Posts { get; set; }
}
Generic repository that implements IGenericRepository
public class EFGenericRepository<T> : IGenericRepository<T> where T : class
{
private DbContext DbContext { get; set; }
private DbSet<T> DbSet { get; set; }
public virtual void Add(T newEntity)
{
DbSet.Add(newEntity);
}
public virtual void Update(T entityToUpdate)
{
DbSet.Attach(entityToUpdate);
DbContext.Entry(entityToUpdate).State = EntityState.Modified;
}
}
Unit of work that implements IUnitOfWork
public class EfUnitOfWork : IUnitOfWork
{
private EFDbContext dbContext;
private IGenericRepository<Post> postRepository;
private IGenericRepository<Tag> tagRepository;
public EfUnitOfWork()
{
this.dbContext = new EFDbContext();
}
public void Commit()
{
dbContext.SaveChanges();
}
public IGenericRepository<Post> PostRepository
{
get
{
if (this.postRepository == null)
{
this.postRepository = new EFGenericRepository<Post>(dbContext);
}
return postRepository;
}
}
public IGenericRepository<Tag> TagRepository
{
get
{
if (this.tagRepository == null)
{
this.tagRepository = new EFGenericRepository<Tag>(dbContext);
}
return tagRepository;
}
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
dbContext.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
service layer
public class ServiceRepository : IServiceRepository
{
private readonly IUnitOfWork UoW;
public ServiceRepository(IUnitOfWork _UoW)
{
UoW = _UoW;
}
public int AddPost(Post post)
{
UoW.PostRepository.Add(post);
UoW.Commit();
return post.Id;
}
public void EditPost(Post post)
{
UoW.PostRepository.Update(post);
UoW.Commit();
}
}
admin controller
public class AdminController : Controller
{
private readonly IServiceRepository _serviceRepository;
public AdminController(IServiceRepository serviceRepository)
{
_serviceRepository = serviceRepository;
}
[HttpPost, ValidateInput(false)]
public ContentResult AddPost(Post post)
{
string json;
ModelState.Clear();
if (TryValidateModel(post))
{
var id = _serviceRepository.AddPost(post);
json = JsonConvert.SerializeObject(new
{
id = id,
success = true,
message = "Post added successfully"
});
}
else
{
json = JsonConvert.SerializeObject(new
{
id = 0,
success = false,
message = "Post was not created"
});
}
return Content(json, "application/json");
}
[HttpPost, ValidateInput(false)]
public ContentResult EditPost(Post post)
{
string json;
ModelState.Clear();
if (TryValidateModel(post))
{
_serviceRepository.EditPost(post, Tags);
json = JsonConvert.SerializeObject(new
{
id = post.Id,
success = true,
message = "Post updated successfully"
});
}
else
{
json = JsonConvert.SerializeObject(new
{
id = 0,
success = false,
message = "Failed to save the changes."
});
}
return Content(json, "application/json");
}
}
I am also using a custom model binder for Post which is as follows
public class PostModelBinder : DefaultModelBinder
{
private readonly IKernel _kernel;
public PostModelBinder(IKernel kernel)
{
_kernel = kernel;
}
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var post = (Post)base.BindModel(controllerContext, bindingContext);
var _blogRepository = _kernel.Get<IServiceRepository>();
}
var tags = bindingContext.ValueProvider.GetValue("Tags").AttemptedValue.Split(',');
if (tags.Length > 0)
{
post.Tags = new List<Tag>();
foreach (var tag in tags)
{
post.Tags.Add(_serviceRepository.Tag( int.Parse(tag.Trim()) ));
}
}
return post;
}
Related
*Update:
Was able to follow this tutorial, and now the error I'm getting states:
"message": "No HTTP resource was found that matches the request URI 'http://localhost:0000/api/v1/Pets('dog')/Color'."
"message": "No routing convention was found to select an action for the OData path with template '~/entityset/key/unresolved'."
Any ideas?*
I am getting an error when trying to retrieve the Color in my Pet query using OData V4.
I'm having quite a bit of trouble, ideally I would use an expand on color (e.g. localhost:0000/api/v1/Pets('dog')?$expand=Colors)
The JSON I need returned is something like:
[
{
"_Key": "1",
"animalname": "dog",
"furcolorname": "black,white",
"color": {
"_Key": "1",
"colorname": "black"
},
{
"_Key": "2",
"colorname": "white"
}
}
]
Maybe I'm on the completely wrong path, either way any input is appreciated!
If I query localhost:0000/api/v1/Pets('dog') :
…
"Message\": \"Invalid column name 'Pet__Key'.\"
…
If I query localhost:0000/api/v1/Pets('dog')?$exand=Colors :
…
"The query specified in the URI is not valid. Could not find a property named 'Colors' on type 'PetShop.Odata.Models.Pet'."
…
Pet.cs model:
namespace PetShop.Odata.Models
{
[Table("pet")]
public class Pet
{
[Key]
public string _Key { get; set; }
public string AnimalName { get; set; }
[ForeignKey("Color")]
public string FurColorName { get; set; }
public virtual Color Color { get; set; }
public virtual ICollection<Color> Colors { get; set; }
}
}
Color.cs model:
namespace PetShop.Odata.Models
{
[Table("color")]
public class Color
{
public Color()
{
new HashSet<Pet>();
}
[Key]
public string _Key { get; set; }
public string ColorName { get; set; }
}
}
PetsController.cs:
namespace PetShop.Odata.Controllers
{
[ApiVersion("1.0")]
[ODataRoutePrefix("Pets")]
[ApiControllerMetricReport]
public class PetsController : ODataController
{
private readonly MyContext context = new MyContext ();
[HttpGet]
[ODataRoute]
[EnableQuery]
[ResponseType(typeof(List<Pet>))]
public IHttpActionResult Get()
{
return Ok(context.Pets.AsQueryable());
}
[EnableQuery]
public IQueryable<Color> Get ([FromODataUri] string key)
{
return context.Pets.Where(m => m._Key == key).SelectMany(a => a.Colors);
}
protected override void Dispose(bool disposing)
{
context.Dispose();
base.Dispose(disposing);
}
}
}
ColorsController.cs:
namespace PetShop.Odata.Controllers
{
[ApiVersion("1.0")]
[ODataRoutePrefix("Colors")]
[ApiControllerMetricReport]
public class ColorsController : ODataController
{
private readonly MyContext context = new MyContext ();
[HttpGet]
[ODataRoute]
[EnableQuery]
[ResponseType(typeof(List<Color>))]
public IHttpActionResult Get()
{
return Ok(context.Colors.AsQueryable());
}
protected override void Dispose(bool disposing)
{
context.Dispose();
base.Dispose(disposing);
}
}
}
PetConfig.cs:
namespace PetShop.Odata.Configuration
{ public class PetModelConfiguration : IModelConfiguration
{
public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
{
builder.EntitySet<Pet>("Pets");
}
}
}
ColorConfig.cs:
namespace PetShop.Odata.Configuration
{ public class ColorModelConfiguration : IModelConfiguration
{
public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
{
builder.EntitySet<Color>("Colors");
}
}
}
MyContext.cs:
…
public DbSet<Pet> Pets { get; set; }
public DbSet<Color> Colors { get; set; }
…
Setup.cs:
…
public static HttpServer CreateHttpServer()
{
var httpConfig = new HttpConfiguration();
var webApiServer = new HttpServer(httpConfig);
httpConfig.AddApiVersioning(options => options.ReportApiVersions = true);
httpConfig.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
var modelBuilder = new VersionedODataModelBuilder(httpConfig)
{
ModelBuilderFactory = () => new ODataConventionModelBuilder().EnableLowerCamelCase(),
ModelConfigurations =
{
new PetConfig(),
new ColorConfig()
},
};
var models = modelBuilder.GetEdmModels();
httpConfig.Count().Filter().OrderBy().Expand().Select().MaxTop(null);
…
Assuming you are using default routing convention, your Get colors method in PetsController.cs does not match the expected OData route format.
Instead of the following:
[EnableQuery]
public IQueryable<Color> Get ([FromODataUri] string key)
{
return context.Pets.Where(m => m._Key == key).SelectMany(a => a.Colors);
}
You should try:
[EnableQuery]
public IQueryable<Color> GetColors ([FromODataUri] string key)
{
return context.Pets.Where(m => m._Key == key).SelectMany(a => a.Colors);
}
This definition would make the OData route: http://localhost:0000/api/v1/Pets('dog')/Colors
For more information on routing conventions see https://learn.microsoft.com/en-us/odata/webapi/built-in-routing-conventions.
A couple alternative approaches exist as well:
Register a different name to a custom nav property
Define a custom OData Function
Having the following context:
public class DemoContext : DbContext
{
private readonly string _connectionStringName = "DemoContext";
public DemoContext() : base("name=DemoContext")
{
//_connectionStringName = "DemoContext";
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
//Database.SetInitializer(new MigrateDatabaseToLatestVersion<DemoContext, Migrations.Configuration>(_connectionStringName));
Database.SetInitializer(new NullDatabaseInitializer<DemoContext>());
}
public DbSet<Employee> Employees { get; set; }
public DbSet<Company> Companies { get; set; }
}
public class Employee
{
[Key]
public int EmployeeId { get; set; }
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
public class Company
{
[Key]
public int CompanyId { get; set; }
public string CompanyName { get; set; }
}
And this services:
public class EmployeeSvc
{
private readonly DemoContext _context;
public EmployeeSvc(Func<DemoContext> context)
{
_context = context();
}
public void Add(EmployeeAgg employee)
{
_context.Employees.Attach(new Employee()
{
Name = employee.Name,
DateOfBirth = employee.DateOfBirth
});
}
public void UpdateAll()
{
var employees = _context.Employees.ToList();
foreach (var employee in employees)
{
employee.Name = $"{Guid.NewGuid()}";
}
}
}
public class CompanySvc
{
private readonly DemoContext _context;
public CompanySvc(Func<DemoContext> context)
{
_context = context();
}
public void Add(CompanyAgg company)
{
_context.Companies.Attach(new Company()
{
CompanyName = company.CompanyName
});
}
public void UpdateAll()
{
var empresas = _context.Companies.ToList();
foreach (var empresa in empresas)
{
empresa.CompanyName = $"{Guid.NewGuid()}";
}
}
}
public class EmployeeAgg
{
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
}
public class CompanyAgg
{
public string CompanyName { get; set; }
}
Anf the following client code:
static void Main(string[] args)
{
var context = new DemoContext();
// ReSharper disable AccessToDisposedClosure
var employeeSvc = new EmployeeSvc(() => context);
var companySvc = new CompanySvc(() => context);
// ReSharper restore AccessToDisposedClosure
Console.WriteLine("Adding entities to context inside services");
employeeSvc.Add(new EmployeeAgg()
{
DateOfBirth = DateTime.Now.AddYears(-10),
Name = $"Employee name"
});
companySvc.Add(new CompanyAgg()
{
CompanyName = $"Company name"
});
employeeSvc.UpdateAll();
companySvc.UpdateAll();
context.SaveChanges();
context.Dispose();
Console.ReadLine();
}
Is it a good practice passing DbContext to application services as delegate in order to have a reference to a single instance and track all changes on it?
Maybe it's your example code, but there's no need here to use a delegate. You could as well just accept a DbContext and pass in the same instance:
public class EmployeeSvc
{
private readonly DemoContext _context;
public EmployeeSvc(DemoContext context)
{
_context = context();
}
// snip ...
}
public class CompanySvc
{
private readonly DemoContext _context;
public CompanySvc(DemoContext context)
{
_context = context();
}
// snip ...
}
var context = new DemoContext();
var employeeSvc = new EmployeeSvc(context);
var companySvc = new CompanySvc(context);
This would give you the exact same result, without needing to pass in a delegate and immediately invoking it.
I suppose however that maybe your actual code is a bit more complicated. If so, you should look into dependency injection and a DI-container.
A DI-container allows you to manage your dependencies and their lifecycles. A common lifecycle is the Singleton lifecycle, where the container will always construct your dependencies passing in the same instance.
if you need to injection your dbContext is better to use interface.
func<> is not suitable.
in Dependency injection interface is suitable.
(excusme for bad english)
I have seen plenty of questions regarding the same topic, but never found solution as yet. The code that is bringing me problems is:
public async Task<IHttpActionResult> Post(FullOrderViewModel fullOrder)
{
//Get all order items
List<OrderItem> orderItems = new List<OrderItem>();
foreach (var oi in fullOrder.Order)
{
var color = _colorRepo.Find(oi.ColorId);
var item = new OrderItem
{
Quantity = oi.Quantity,
Color = color
};
orderItems.Add(item);
}
//Get customer id
var customer = await _adminRepo.GetCustomerByUserName(fullOrder.Customer.UserName);
var billAddress = _addressRepo.All.FirstOrDefault(x => x.AddressLine == fullOrder.BillingAddress.AddressLine && x.PostalCode == fullOrder.BillingAddress.PostalCode);
var deliveryAddress = _addressRepo.All.FirstOrDefault(x => x.AddressLine == fullOrder.DeliveryAddress.AddressLine && x.PostalCode == fullOrder.DeliveryAddress.PostalCode);
//CASE : sample order
if (fullOrder.OrderType == OrderType.Sample)
{
var order = new SampleOrder {
Type = OrderType.Sample,
Status = "Unauthorized",
Origin = Origin.Online,
OrderItems = orderItems,
CreationDate = DateTime.Now,
CustomerId = customer.Id,
BillAddressId = billAddress.AddressId,
DeliveryAddressId = deliveryAddress.AddressId
};
try
{
_sampleOrderRepo.InserGraph(order);
_unitOfWork.Save();
}
catch (Exception e)
{
return InternalServerError(e);
}
The way I get my repositories is via dependency injection in the OrderController constructor, so in a standard way as I believe. Each of my repos is build in this way:
public class SampleOrderRepository : EntityRepository<SampleOrder, IPhoeniceUnitOfWork<PhoeniceContext>>, ISampleOrderRepository
{
public SampleOrderRepository(IPhoeniceUnitOfWork<PhoeniceContext> unitOfWork) : base(unitOfWork)
{
}
protected override IDbSet<SampleOrder> List(IPhoeniceUnitOfWork<PhoeniceContext> unitOfWork)
{
return unitOfWork.Context.SampleOrders;
}
protected override Expression<Func<SampleOrder, bool>> FindInt(int id)
{
return x => x.OrderId == id;
}
}
Where I override methods from general entity repository:
public abstract class EntityRepository<T, TContext> : IEntityRepository<T>
where T : class, IObjectWithState
where TContext : IUnitOfWork<EntityContext>
{
protected readonly TContext _unitOfWork;
protected EntityRepository(TContext unitOfWork)
{
_unitOfWork = unitOfWork;
}
protected virtual IDbSet<T> List(TContext unitOfWork)
{
throw new NotImplementedException("List operation not implemented!");
}
protected virtual Expression<Func<T, bool>> FindInt(int id)
{
throw new NotImplementedException("Finding entity with an int is not implemented!");
}
protected virtual Expression<Func<T, bool>> FindString(string id)
{
throw new NotImplementedException("Finding entity with an int is not implemented!");
}
public virtual IQueryable<T> All { get { return List(_unitOfWork); } }
public virtual IQueryable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties)
{
return includeProperties.Aggregate(All, (current, includeProperty) => current.Include(includeProperty));
}
public virtual T Find(int id)
{
return All.FirstOrDefault(FindInt(id));
}
public virtual T FindByString(string id)
{
return All.FirstOrDefault(FindString(id));
}
public virtual void InserGraph(T entity)
{
List(_unitOfWork).Add(entity);
}
public void InsertOrUpdate(T entity)
{
if (entity.ObjectState == State.Added) // New entity
{
_unitOfWork.Context.Entry(entity).State = EntityState.Added;
}
else // Existing entity
{
_unitOfWork.Context.Entry(entity).State = EntityState.Modified;
_unitOfWork.Context.ApplyStateChanges();
}
}
public virtual void Delete(int id)
{
var entity = Find(id);
List(_unitOfWork).Remove(entity);
}
public void Dispose()
{
}
}
Finally this is my UnitOfWork item:
public class PhoeniceUnitOfWork : IPhoeniceUnitOfWork<PhoeniceContext>
{
private readonly PhoeniceContext _context;
public PhoeniceUnitOfWork() : base()
{
_context = new PhoeniceContext();
}
//Left for testing purposes
public PhoeniceUnitOfWork(PhoeniceContext context)
{
_context = context;
}
public int Save()
{
return _context.SaveChanges();
}
public void Dispose()
{
_context.Dispose();
}
public IAddressRepository AddressRepository
{
get { return new AddressRepository(this); }
}
public IColorRepository ColorRepository
{
get { return new ColorRepository(this); }
}
public IHideOrderRepository HideOrderReposiotory
{
get { return new HideOrderRepository(this); }
}
public ISampleOrderRepository SampleOrderRepository
{
get { return new SampleOrderRepository(this); }
}
public ITaxonomyRepository TaxonomyRepository
{
get { return new TaxonomyRepository(this); }
}
PhoeniceContext IUnitOfWork<PhoeniceContext>.Context
{
get { return _context; }
}
}
and context that I'm using
public class PhoeniceContext : EntityContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, add the following
// code to the Application_Start method in your Global.asax file.
// Note: this will destroy and re-create your database with every model change.
//
// System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<Phoenice.EntityFramework.Models.PhoeniceContext>());
public PhoeniceContext()
: base("Phoenice")
{
Database.SetInitializer<PhoeniceContext>(new DropCreateIfChangeInitializer());
//Database.SetInitializer<PhoeniceContext>(new DropCreateDatabaseAlways());
}
public DbSet<Address> Addresses { get; set; }
public DbSet<SampleOrder> SampleOrders { get; set; }
public DbSet<HideOrder> HideOrders { get; set; }
public DbSet<OrderItem> OrderItems { get; set; }
public DbSet<Color> Colors { get; set; }
public DbSet<Taxonomy> Taxonomies { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//There is OnModelCreating already overwritten in IdentityDbContext
//with all details required for ASP.NET identity
base.OnModelCreating(modelBuilder);
//solution for this mapping: http://stackoverflow.com/questions/11632951/primary-key-violation-inheritance-using-ef-code-first
modelBuilder.Entity<SampleOrder>()
.HasKey(x => x.OrderId)
.Map(m =>
{
//Requires EF 6.1
m.MapInheritedProperties();
m.ToTable("SampleOrders");
})
.Property(x => x.OrderId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<HideOrder>().Map(m =>
{
//Requires EF 6.1
m.MapInheritedProperties();
m.ToTable("HideOrders");
});
modelBuilder.Entity<HideOrder>().HasKey(x => x.OrderId);
}
}
I know that it's a lot of code that I am showing, but hope these are all of the building blocks for data access layer. For some reason when I call InsertGraph method on SampleOrders I get "An entity object cannot be referenced by multiple instances of IEntityChangeTracker" exception. I looked through this code dozens of times, and cannot get why does it throw this error message
i'm not sure of my answer, but i will suggest you the following :
my version of entity Framework is a bit different so i'm not sure you'll have all required functionnality. When you do this
List<OrderItem> orderItems = new List<OrderItem>();
foreach (var oi in fullOrder.Order)
{
var color = _colorRepo.Find(oi.ColorId);
var item = new OrderItem
{
Quantity = oi.Quantity,
Color = color
};
orderItems.Add(item);
}
I think you should first detach all your oi objects then the code should be something like
List<OrderItem> orderItems = new List<OrderItem>();
foreach (var oi in fullOrder.Order)
{
//detach oi
//attach oi
var color = _colorRepo.Find(oi.ColorId);
var item = new OrderItem
{
Quantity = oi.Quantity,
Color = color
};
orderItems.Add(item);
}
The items in your orderItems contains color. Which is returned by:
_colorRepo.Find(oi.ColorId);
The colorRepo holds the entities of color.
The order in the next line is used by another repo, but in 'order' there are colors from the previous repo.
_sampleOrderRepo.InserGraph(order);
Two repo's are holding the same entity object which causes this exception.
To avoid this problem you can two things:
Create a new object of Color every time you use _colorRepo.Find():
var color = new Color()
{
id = oi.ColorId
}
or use only one repo by moving the functionality to the services
I'm working on a project that consists of multiple objects that I want to save to my database. I'm using a single context and a series of repository classes to access them.
When I try to save an entity, it seems to save all the virtual entities associated with it, even if that entity exists in the database already.
These are my classes:
public class Requirement
{
public int ID { get; set; }
public DateTime DateDue { get; set; }
public DateTime DateCompleted { get; set; }
public virtual Standard Standard { get; set; }
public virtual Project Project { get; set; }
}
public class Standard
{
public int ID { get; set; }
public int AgencyID { get; set; }
public string Name { get; set; }
public virtual Agency Agency { get; set; }
}
public class Project
{
public int ID { get; set; }
public bool Active { get; set; }
public virtual Agency Agency { get; set; }
public virtual Department Department { get; set; }
}
And this is the method I have for creating some data:
public class RequirementRepository
{
public static string CreateMockData()
{
StandardRepository stdRep = new StandardRepository();
ProjectRepository projRep = new ProjectRepository();
RequirementRepository reqRep = new RequirementRepository();
Project project = projRep.Find(1);
StringBuilder sb = new StringBuilder()
foreach (Standard s in stdRep.FindByAgencyID(project.Agency.ID))
{
Requirement r = new Requirement();
r.Project = project;
r.Standard = s;
r.DateCompleted = (DateTime)SqlDateTime.MaxValue;
r.DateDue = DateTime.Now.AddDays(90);
r = reqRep.Save(r);
sb.AppendLine(String.Format("Saved Requirement ID {0} with Project ID {1}<br>", r.ID, r.Project.ID));
}
return sb.ToString();
}
}
And here is associated repository code:
public class ProjectRepository
{
public Project Find(int id)
{
using (var db = new MyContext())
{
return db.Projects
.Include(p => p.Agency)
.Include(p => p.Department)
.First(p => p.ID.Equals(id));
}
}
}
public class StandardRepository
{
public List<Standard> FindByAgencyID(int agencyID)
{
using (var db = new MyContext())
{
return db.Standards.Where(r => r.AgencyID == agencyID).ToList();
}
}
}
public class RequirementRepository
{
public Requirement Save(Requirement requirement)
{
using (var db = new MyContext())
{
Requirement retVal = requirement;
if (requirement.ID.Equals(0))
{
retVal = db.Requirements.Add(requirement);
}
else
{
db.Entry(requirement).State = EntityState.Modified;
}
db.SaveChanges();
return retVal;
}
}
}
When I run this method, I expect it to insert a number of new Requirements into the database with the project ID of 1 and a standard ID of whatever standard it's on. Instead, it creates a whole new project and a whole new standard for every requirement it adds, then assigns those IDs to the requirement.
Each context keeps track of the entities loaded, modified and the entities added.
Your repositories need to look something like this....
public class StandardRepository
{
MyContext _context;
public StandardRepository(MyContext context)
{
_context = context;
}
public List<Standard> FindByAgencyID(int agencyID)
{
return _context.Standards.Where(r => r.AgencyID == agencyID).ToList();
}
}
public class RequirementRepository
{
MyContext _context;
public RequirementRepository(MyContext context)
{
_context = context;
}
public Requirement Save(Requirement requirement)
{
Requirement retVal = requirement;
if (requirement.ID.Equals(0))
{
retVal = _context.Requirements.Add(requirement);
}
else
{
_context.Entry(requirement).State = EntityState.Modified;
}
_context.SaveChanges();
return retVal;
}
}
public class RequirementRepository
{
public static string CreateMockData()
{
using(MyContext context = new MyContext())
{
StandardRepository stdRep = new StandardRepository(context);
ProjectRepository projRep = new ProjectRepository(context);
RequirementRepository reqRep = new RequirementRepository(context);
Project project = projRep.Find(1);
StringBuilder sb = new StringBuilder()
foreach (Standard s in stdRep.FindByAgencyID(project.Agency.ID))
{
Requirement r = new Requirement();
r.Project = project;
r.Standard = s;
r.DateCompleted = (DateTime)SqlDateTime.MaxValue;
r.DateDue = DateTime.Now.AddDays(90);
r = reqRep.Save(r);
sb.AppendLine(String.Format("Saved Requirement ID {0} with Project ID {1}<br>", r.ID, r.Project.ID));
}
}
return sb.ToString();
}
}
From my understanding, you shouldn't have to manually set the state of the object you have modified unless you have detached it from its context. EF keeps track of the object state.
I like to use something like this:
public abstract class EntityRepositoryBase<TEntity> : IDisposable, IEntityRepositoryBase<TEntity> where TEntity : class , IEntityWithId
{
protected EntityRepositoryBase()
{
Context = new SomeEntities();
}
public abstract ObjectSet<TEntity> EntityCollection { get; }
public SomeEntities Context { get; set; }
public TEntity GetById(int id)
{
return EntityCollection
.FirstOrDefault(x => x.Id == id);
}
public void Dispose()
{
Context.Dispose();
}
}
Then in the deriving repositories:
public class AnswerRepository : EntityRepositoryBase<AnswerEntity>, IAnswerRepository
{
public override ObjectSet<AnswerEntity> EntityCollection
{
get { return Context.AnswerEntities; }
}
}
I inject the Repositories into the relevant class using ninject but you should be able to get similar with:
using (var repo = new AnswerRepository())
{
// modifying via Context
var someEntity = repo.GetById(someId);
someEntity.Value = "1";
repo.Context.SaveChanges();
//modifying via repo
repo.Delete(anotherEntity);
}
and then doing what you need to do. The context is exposed via the interface IEntityRepositoryBase should you need to perform out-of-repository modifications and then SaveChanges() as well as any specific CRUD type methods in your repo. Once out of scope the object and the underlying connection will be closed.
I'm struggling with a problem that never occurred to me until now..
I tried to implement an application with EF Code First with SQL CE Database.
I have these two main class that give me the specific problem:
public class Session
{
public int Id { get; set; }
public Guid Unique { get; set; }
public DateTime DateTime { get; set; }
public virtual Patient Patient { get; set; }
public virtual Test Test { get; set; }
public virtual List<VideoExerciseResult> VideoSessionResults { get; set; }
}
and
public class VideoExerciseResult
{
public int Id { get; set; }
public Guid Unique { get; set; }
public string Word { get; set; }
public bool IsAnswerCorrect { get; set; }
public virtual Session Session { get; set; }
}
When I finish a session and save the results EF i sable to complete the operation and save both the session and the results on the db.
I can check the fields looking directly in the .sdf files and the references to the Session_Id in the VideoExerciseResults column are present.
But when in the code I try to make this operation:
private void GetSessionData()
{
List<VideoExerciseResult> tempList2 = new List<VideoExerciseResult>(UOW.VideoSessionsResults.GetAll());
ListOfVideoSessionResults = new ObservableCollection<VideoExerciseResult>((tempList2.Where(ver => ver.Session.Id == SelectedSession.Id)).ToList());
}
It gives me error with the message:
Object reference not set to an instance of an object.
And in fact if I mouse hover on tempList2 some of the videoexerciseresult objects have the Session set to null, even thought there is a value in the .sdf file..
Did i miss something?
I will add the code regarding Session...
It's based on a Repository and Unit Of Work schema I followed
public class EnPleinProjectUOW : IEnPleinProjectUOW, IDisposable
{
private EnPleinProjectDbContext DbContext { get; set; }
public IRepositoryProvider RepositoryProvider { get; set; }
public IVideoExerciseResultRepository VideoSessionsResults
{
get { return GetRepo<IVideoExerciseResultRepository>(); }
}
public IPatientRepository Patients
{
get { return GetRepo<IPatientRepository>(); }
}
public ISessionRepository Sessions
{
get { return GetRepo<ISessionRepository>(); }
}
public ITestRepository Tests
{
get { return GetRepo<ITestRepository>(); }
}
public IImageFileRepository ImageFiles
{
get { return GetRepo<IImageFileRepository>(); }
}
// We need inverse of control
public EnPleinProjectUOW(IRepositoryProvider repositoryProvider)
{
CreateDbcontext();
repositoryProvider.DbContext = DbContext;
RepositoryProvider = repositoryProvider;
}
private void CreateDbcontext()
{
DbContext = new EnPleinProjectDbContext();
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Configuration.LazyLoadingEnabled = false;
DbContext.Configuration.ValidateOnSaveEnabled = false;
}
private IRepository<T> GetStandardRepo<T>() where T : class
{
return RepositoryProvider.GetRepositoryForEntityType<T>();
}
private T GetRepo<T>() where T : class
{
return RepositoryProvider.GetRepository<T>();
}
public void Commit()
{
DbContext.SaveChanges();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (DbContext != null)
{
DbContext.Dispose();
}
}
}
}
And this is for the general commands:
public class EnPleinProjectRepository<T> : IRepository<T> where T : class
{
#region Properties
protected DbContext DbContext { get; set; }
protected DbSet<T> DbSet { get; set; }
#endregion
#region Constructor
public EnPleinProjectRepository(DbContext dbContext)
{
if (dbContext == null)
throw new ArgumentNullException("Dbcontext missing");
DbContext = dbContext;
DbSet = DbContext.Set<T>();
}
#endregion
#region Methods
public IQueryable<T> GetAll()
{
return DbSet;
}
public T GetById(int id)
{
return DbSet.Find(id);
}
public void Add(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State != System.Data.EntityState.Detached)
dbEntityEntry.State = System.Data.EntityState.Added;
else
{
DbSet.Add(entity);
}
}
public void Update(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State == System.Data.EntityState.Detached)
{
DbSet.Attach(entity);
}
dbEntityEntry.State = System.Data.EntityState.Modified;
}
public void Delete(T entity)
{
DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
if (dbEntityEntry.State != System.Data.EntityState.Deleted)
{
dbEntityEntry.State = System.Data.EntityState.Deleted;
}
else
{
DbSet.Attach(entity);
DbSet.Remove(entity);
}
}
public void Delete(int id)
{
var entity = GetById(id);
if (entity == null) return;
Delete(entity);
}
public void DeleteAll()
{
foreach (T entity in DbSet)
Delete(entity);
}
But I', quite confident that this works since in the Database the data are saved and all the references... I don't know why some of them are not read back...
Thank you for the answers
I discovered that doing the expression in a single line works:
ListOfVideoSessionResults = new ObservableCollection<VideoExerciseResult>(UOW.VideoSessionsResults.GetAll().Where(vsr => vsr.Session.Id == SelectedSession.Id));