Hi, I would like your option on this interface that I use to access Model specific information in a db context. Of particular interests in whether you think my edit method makes any sense
using System.Data;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
namespace Domain.Concrete
{
public class CustomProfileProvider : ICustomProfileProvider
{
private readonly _DB context = new _DB();
#region ICustomProfileProvider Members
public IQueryable<CustomProfile> Read()
{
return context.CustomProfiles;
}
public CustomProfile Find(string username)
{
return context.CustomProfiles.Find(username);
}
public CustomProfile Create(CustomProfile customProfile)
{
CustomProfile profile = context.CustomProfiles.Add(customProfile);
try
{
context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (DbEntityValidationResult validationErrors in dbEx.EntityValidationErrors)
{
foreach (DbValidationError validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName,
validationError.ErrorMessage);
}
}
}
return profile;
}
public void Edit(CustomProfile customProfile)
{
context.Entry(customProfile).State = EntityState.Modified; context.SaveChanges(); }
From the syntax looks nice. Hope you should pass an instance that is already with the same primary key in the existing db of its records. Thus you can do updating with it. And the instance should come from the same dbcontent.
GorillaMann
Member
117 Points
332 Posts
Code review for implementation for DB context
Nov 12, 2012 08:48 PM|LINK
Hi, I would like your option on this interface that I use to access Model specific information in a db context. Of particular interests in whether you think my edit method makes any sense
using System.Data; using System.Data.Entity.Validation; using System.Diagnostics; using System.Linq; namespace Domain.Concrete { public class CustomProfileProvider : ICustomProfileProvider { private readonly _DB context = new _DB(); #region ICustomProfileProvider Members public IQueryable<CustomProfile> Read() { return context.CustomProfiles; } public CustomProfile Find(string username) { return context.CustomProfiles.Find(username); } public CustomProfile Create(CustomProfile customProfile) { CustomProfile profile = context.CustomProfiles.Add(customProfile); try { context.SaveChanges(); } catch (DbEntityValidationException dbEx) { foreach (DbEntityValidationResult validationErrors in dbEx.EntityValidationErrors) { foreach (DbValidationError validationError in validationErrors.ValidationErrors) { Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); } } } return profile; }public void Edit(CustomProfile customProfile) { context.Entry(customProfile).State = EntityState.Modified;context.SaveChanges();
}
thaicarrot
Contributor
5433 Points
1508 Posts
Re: Code review for implementation for DB context
Nov 12, 2012 09:35 PM|LINK
public void Edit(CustomProfile customProfile)
{
context.Entry(customProfile).State = EntityState.Modified; context.SaveChanges(); }??
Okay,
Weera
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Code review for implementation for DB context
Nov 14, 2012 12:35 AM|LINK
Hi,
From the syntax looks nice. Hope you should pass an instance that is already with the same primary key in the existing db of its records. Thus you can do updating with it. And the instance should come from the same dbcontent.
Reguards!