我有一个正在做的项目。我决定在DBContext生成器中使用实体框架(为了保持不知情的对象),现在我遇到了一些问题。
以下是我如何设置解决方案:
解决方案:
以下是我的问题:(这一切我都是新手)
假设我有对象(实体)作业,我想在这个对象上定义select、insert、update和delete方法。我在哪里这样做呢?我尝试在BusinessObjects项目中创建文件夹自定义(在这里我会把所有的自定义都放在这里)。然后,我想在那里定义我的方法,但我不知道如何创建ctx (context)对象的新实例。
这是我的代码:
namespace BusinessObjects
{
public partial class Job
{
public Job GetJob(Guid Id) {
using (var ctx = new BestGigEntities())
}
return null; //for now
}
}
}我得到的错误消息是BestGigEntities不存在于命名空间中。BestGigEntities应该住在BusinessObjects中,但是当我尝试通过BusinessObject项目访问它时,它是不可见的。但我可以从我的主要网络项目中看到它。在myproject.Context.tt中,BusinessObjects被指定为自定义工具名称空间。为什么我看不见?
我已经检查了我的myproject.Context.cs文件,我可以看到
public partial class BestGigEntities : DbContext
{
public BestGigEntities()
: base("name=BestGigEntities")
{
. ...一切似乎都很好。我几乎可以肯定,我正确地添加了所有参考资料。我在想,也许我是想在错误的地方定义这些方法?
BestGigEntities在我的网络项目中是可见的,我可以从那里使用它。
任何帮助都是非常感谢的。
发布于 2011-12-13 01:03:22
我不建议您扩展模型的部分类。如果你熟悉MVVM模式并使用它,那就更好了。
这里被描述为一些类,比如服务和帮助。他们可能会提示你从哪里开始使项目更有条理。
在最简单的情况下,CRUD选项看起来就像
public static Job Get(int jobId)
{
using (var context = new BestGigEntities())
{
return context.Jobs.FirstOrDefault(s => s.Id == jobId);
}
}
public static void Save(Job job)
{
using (var context = new BestGigEntities())
{
context.Jobs.Attach(job);
context.Entry(job).State = EntityState.Modified;
context.SaveChanges();
}
}
public static void Create(Job job)
{
using (var context = new BestGigEntities())
{
context.Jobs.Add(job);
context.SaveChanges();
}
}
public static void Delete(Job job)
{
using (var context = new BestGigEntities())
{
context.Entry(job).State = EntityState.Deleted;
context.SaveChanges();
}
} https://stackoverflow.com/questions/8481970
复制相似问题