Tag: entity framework

Unity DI使用PerRequestLifetimeManager注入DbContext

我有以下代码用Unity初始化实例: IUnityContainer container = new UnityContainer(); container.RegisterType(new PerRequestLifetimeManager(), new InjectionConstructor()); container.RegisterType(typeof(IGenericRepository), typeof(GenericRepository)); container.RegisterType(new PerRequestLifetimeManager()); container.RegisterTypes( AllClasses.FromAssemblies( Assembly.GetAssembly(typeof(IUserService)), Assembly.GetAssembly(typeof(UserService))), WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.PerResolve); DependencyResolver.SetResolver(new Unity.Mvc4.UnityDependencyResolver(container)); GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container); 我使用PerRequestLifetimeManager所以我按照MSDN上的建议,并在上面代码的末尾添加了新行: DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule)); 但在我放置之后。 当加载页面(仅静态html)时,我向我的WebApi控制器发送ajax请求,该控制器调用抛出错误的GenericReposirory Get()方法: The operation cannot be completed because the DbContext has been disposed. 没有这行代码,一切正常,但没有设置它可能不会处置上下文。 我的UnitOfWork类: public class UnitOfWork : IUnitOfWork, IDisposable { private readonly VotingSystemContext […]

在SQL数据库和EF6中保存标志枚举。 这可能吗?

在ASP.NET应用程序上,我有一个标志枚举如下: [Flags] enum Target : int { None = 0, Group = 1, Student = 2, Professor = 4, All = Group | Student | Professor } 我可以使用Entity Framework 6在SQL数据库表中保存包含多个项目的值吗? 怎么会得救? 谢谢你,米格尔

Asp.net MVC – 如何哈希密码

如何将用户输入(密码)哈希到数据库,然后在登录期间读取哈希密码? 我相信解决方案是在注册时散列密码,其中密码在db中保存为散列。 登录后,它应该取消哈希并将其密码与用户密码输入进行比较。 但我不知道该怎么做。 我允许密码在db中具有nvarchar(MAX) ,因为散列密码通常很长。 [Required] [StringLength(MAX, MinimumLength = 3, ErrorMessage = “min 3, max 50 letters”)] public string Password { get; set; } 寄存器: [HttpPost] public ActionResult Register(User user) { if (ModelState.IsValid) { var u = new User { UserName = user.UserName, Password = user.Password }; db.Users.Add(u); db.SaveChanges(); return RedirectToAction(“Login”); } }return View(); […]

EF与POCO + WCF + WPF。 在客户端重用POCO课程还是使用DTO?

我们正在开发一个带有WPF客户端的3层应用程序,它通过WCF与BLL进行通信。 我们使用EF访问我们的数据库。 我们一直在使用EF的默认EntityObject代码生成器,但是当通过线路发送这些对象时,以及在BLL中处理和重新附加它们时会遇到很多问题和序列化问题。 我们即将切换到POCO模板,并重写数据访问和我们应用程序的通信部分(我们希望有一个更清洁的架构,而不是那种“魔术代码”。 我的问题是在客户端重用POCO类是否是个好主意? 或者我们应该创建单独的DTO类? 即使它们与POCO实体类相同? 这两种方法的优点/缺点是什么?

加载要在WrapPanel中显示的大量图像

我正在使用Entity Framework Code First 我有这样的电影 : public class Movie { public byte[] Thumbnail { get; set; } public int MovieId { get; set; } } 电影的集合是这样的: public class NorthwindContext : DbContext { public DbSet Movies { get; set; } } 我有一个像这样的MovieViewModel : public class MovieViewModel { private readonly Movie _movie; public MovieViewModel(Movie movie) { _movieModel […]

如何使用EF Core 2.1.0和代理进行延迟加载

我有以下型号: public class Session { public int SessionID { get; set; } public int UserID { get; set; } public virtual User User { get; set; } } public class User { public int UserID { get; set; } public int OrganizationID { get; set; } public virtual ICollection Sessions { get; set; } public […]

我可以在Entity Framework SubQuery中使用扩展方法吗?

我正在尝试使用Entity Framework整合访问不同表的逻辑。 我创建了一个扩展方法,用于从我的注册实体中提取所有注册人: public static IEnumerable Attending(this IEnumerable registrations) { return registrations.Where(r => r.Status == RegistrationStatus.Paid || r.Status == RegistrationStatus.Assigned || r.Status == RegistrationStatus.Completed); } 这适用于这样的查询: var attendees = db.Registrations.Attending().ToList(); 但是在子查询中使用它时不起作用: ProductTotals = db.Products.Where(p => p.EventID == ev.Id).Select(p => new ProductSummaryViewModel { ProductID = p.ProductID, ProductName = p.Name, Registrations = p.Registrations.Attending().Count(), }).ToList(); 我收到以下错误: LINQ to […]

“IEntityChangeTracker的多个实例无法引用实体对象。”

我使用MYSql服务器作为我的Windows窗体应用程序后面的数据库。 我的数据库中有两个模式,我必须将条目输入。 我为每个模式创建了两个上下文对象。 当我使用schema1上的contextA时,所有条目都完美地完成,但是当我使用contextB时,我得到了这个exception。 它与MySql Driver有关吗?

无法首先使用Npgsql和Entity Framework代码在PostreSQL中创建数据库

我正在尝试设置我的应用程序以使用Entity Framework和PostgreSQL,但我遇到了一个问题。 我已经通过nuget添加了Npqsql,并将以下提供程序工厂添加到web.config : 使用连接字符串: 它似乎很好地连接到数据库,但当我尝试在数据库上执行任何类型的操作时,我得到以下exception: FATAL: 3D000: database “withoomph” does not exist 当db设置如下时,我正在正确设置数据库intitializer: static MyDB() { Database.SetInitializer(new CreateDatabaseIfNotExists()); } 所以当我尝试用我的DbContext做任何事情时,它应该只是简单地创建db? 我不明白,整个早上都拔我的头发!

EFCode First Property Null问题

我在asp.net mvc 3模型中使用EFCode First。 (Entity Framework 4.0和EFCode First 0.8)模型定义如下: public class User { [Key] public int Id { get; set; } public string Name { get; set; } public int WorkedYears { get; set; } } 当使用db.Users.Find(1) ,会抛出此错误: ‘User’上的’WorkedYears’属性无法设置为’null’值。 您必须将此属性设置为“Int32”类型的非null值。 注意:user.Id = 1存在于数据库中,记录的WorkedYears为NULL。 如果我在数据库中设置WorkedYears = 0,则错误将消失,如果我将属性定义为: public int? WorkedYears{get; set;} public int? WorkedYears{get; set;} public […]