Entity framework not creating insert statement
Wonder if you could help me with something I have been scratching my head for some time with. I'll begin with the code first!
public class DevelopmentImageContext : Context<DevelopmentImage>
{
public List<DevelopmentImage> DevelopmentImages { get; private set; }
public DevelopmentImageContext() { }
public DevelopmentImageContext(int imageId)
{
EntityId = imageId;
}
public DevelopmentImageContext(Development development)
{
var result = (from d in Context.DevelopmentImages
where d.Development == development
select d).ToList();
DevelopmentImages = result;
}
public override DevelopmentImage GetEntity()
{
if (EntityId != 0)
{
return Context.DevelopmentImages.Where(d => d.ImageId == EntityId).FirstOrDefault();
}
throw new EntityNotFound("Entity was either null or 0");
}
public override void CreateEntity(DevelopmentImage entity)
{
Context.DevelopmentImages.AddObject(entity);
Context.SaveChanges();
}
public override void CreateEntities(List<DevelopmentImage> entities)
{
foreach (var entity in entities)
{
try
{
Context.DevelopmentImages.AddObject(entity);
CommitChanges();
}
catch (Exception exception)
{
throw new Exception("Unable to create Object", exception);
}
}
Context.SaveChanges();
}
}
There is a abstract class called ContextManager with a property called Context. I can post this if necessary.
When calling the CreateEntities method, I pass through a list of Development, loop through them and insert into the database. Problem is, nothing is created. Tried using EFProf to see if it creates a transaction with the insert query and nothing. I do have another method in the Context class called DeleteEntities which effectly deletes all the data in the data using some basic SQL and t开发者_如何学编程he ExecuteStoreCommand method. Within EFProf I can see this SQL being executed.
Any ideas why I cannot create the object?
Thanks, Matt
Edit: I have renamed the property Context to ContextEntity to see if this changes anything and no :(
Did you check your db connection string? Since you are using code first, it is probably creating a new database and everything seems to work fine. Best to provide the connection string explicitly.
If you happen to have no configuration file, a default database will be used:
Data Source=.\SQLEXPRESS;Initial Catalog=MyNamespace.MyContext;Integrated Security=True;MultipleActiveResultSets=True
To find out:
using (BlogContext context = new BlogContext())
{
string cs = context.Database.Connection.ConnectionString;
}
I feel massivly stupid. I sould have posted the ContextManager class because someone would have spoted my mistake:
private static GirlingsWebEntities _entity;
public GirlingsWebEntities ContextEntity
{
get
{
if (HttpContext.Current == null)
{
return _entity ?? (_entity = new GirlingsWebEntities());
}
else
{
string sKey = "context_" + HttpContext.Current.GetHashCode().ToString("x");
if (!HttpContext.Current.Items.Contains(sKey))
{
HttpContext.Current.Items.Add(sKey, new GirlingsWebEntities());
((GirlingsWebEntities)HttpContext.Current.Items[sKey]).ContextOptions.LazyLoadingEnabled = true;
}
return (GirlingsWebEntities)HttpContext.Current.Items[sKey];
}
}
}
public void CommitChanges()
{
ContextEntity.SaveChanges();
}
}
where I have return _entity ?? (_entity = new GirlingsWebEntities()); I was just returning a normal property and not a static property so therefore using a different connection to the database, one that didn't exist!
Hope this helps someone else out :)
精彩评论