Tag: entity framework

entity framework自动更新

我尝试将Entity-Framework实现到我的项目中! 我的项目是基于插件的,所以我不知道我必须保存到数据库的哪个对象。 我已经实现了它: public class DatabaseContext : DbContext { public DatabaseContext() : base() { Database.Initialize(true); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { foreach( PluginDto plugin in BackendContext.Current.PluginManager._plugins) { foreach(Type obj in plugin.plugin.getPluginDatabaseObjects()) { Type typ = typeof(EntityTypeConfiguration).MakeGenericType(obj); List l = modelBuilder.GetType().GetMethods().ToList(); MethodInfo m_Entitiy = modelBuilder.GetType().GetMethod(“Entity”).MakeGenericMethod(new Type[] { obj }); var configObj = m_Entitiy.Invoke(modelBuilder, null); MethodInfo […]

如何在ADO.Net实体模型中将具有外键的表更新到另一个表?

我有2个表,Table1有一个主键’CustomizationId’,而Table2有一个与之匹配的FK Customizationid。 表2没有主键。 我正在尝试从基于Web的表单添加新记录。 我试图将其保存到数据库,我收到一个错误: Customization customization = new Customization(); Code code = new Code(); customization.Name = CustomizationName.Text; customization.LastUpdated = DateTime.Now; code.Top = top_js.InnerText; code.Bottom = bottom_js.InnerText; //code.CustomizationId = customization.CustomizationId; customization.Code = code; entities.AddToCustomizations(customization); entities.SaveChanges(); 当我调用SaveChanges时,无论是否添加注释行,我都会收到错误。 Unable to update the EntitySet ‘Code’ because it has a DefiningQuery and no element exists in the element to […]

entity framework – 包含导航属性的选择条件

假设我有这些简化的EF生成实体…… public class PurchaseOrder { public int POID {get;set;} public int OrderID {get;set;} public int VendorID {get;set;} public IEnumerable Orders {get;set;} } public class Order { public int OrderID {get;set;} public decimal Price {get;set;} public IEnumerable Items {get;set;} } public class Item { public int OrderID {get; set;} public string SKU {get;set;} public int VendorID […]

我应该将entity framework视为非托管资源吗?

我正在使用一个在其构造函数中使用EF引用的类。 我已经实现了IDisposable ,但我不确定我是否需要析构函数,因为我不确定我是否可以将EF归类为非托管资源。 如果EF是托管资源,那么我不需要析构函数,所以我认为这是一个恰当的例子: public ExampleClass : IDisposable { public ExampleClass(string connectionStringName, ILogger log) { //… Db = new Entities(connectionStringName); } private bool _isDisposed; public void Dispose() { if (_isDisposed) return; Db.Dispose(); _isDisposed= true; } } 如果EF不受管理,那么我将继续这样做: public ExampleClass : IDisposable { public ExampleClass(string connectionStringName, ILogger log) { //… Db = new Entities(connectionStringName); } public […]

自动生成的默认连接字符串与手动添加的字符串

假设我使用Entity Framework Code First创建了简单的WPF应用程序来创建数据库,连接到它并显示一些数据。 从一开始我就不想担心连接字符串了,所以在通过Nuget添加entityframework引用之后我会得到自动生成的app.config,如下所示: 我将运行test并观察连接字符串: var strings = ConfigurationManager.ConnectionStrings; 结果: [0] = {data source=.\SQLEXPRESS;Integrated Security=SSPI;attachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true} 由于我想定义自己的连接字符串,我将把它添加到我的app.config中: 因此,当我再次运行测试并观察连接时,我可以看到现在有两个: [0] = {data source=.\SQLEXPRESS;Integrated Security=SSPI;attachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true} [1] = {data source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\myDb.mdf;Integrated Security=True} 为什么我能看到两个连接字符串? 如果第一个是默认的,一旦我创建了它,是否应该忘记它? 谢谢

使用DateDiff和Linq.Dynamic库获取今天的记录

我试图通过MVC 5 / Entity Framework 6应用程序中的Linq表达式使用DateDiff SQL语法获取今天添加的所有记录。 DateDiff函数抛出运行时错误 实际上我想用以下linq WHERE子句来解析linq动态 .Where(p => DbFunctions.DiffDays(p.added_date, DateTime.Now) == 0) 为了获取今天添加的记录。 我正在使用的示例代码如下所示 var _list = new vsk_error_log(); using (var entities = new vskdbEntities()) { _list = entities.vsk_error_log //.Where(“DateDiff(DAY,added_date,getdate())=0”) .Where(p => DbFunctions.DiffDays(p.added_date, DateTime.Now) == 0) .ToList(); } return _list; 关于Linq.Dynamic表达式 – 如何编写where子句 LINQ中的动态WHERE子句

Dot Net Entity Framework数据库更新不会在mysql数据库中创建表

我正在使用MySql作为官方连接提供程序的数据库。 我正在尝试使用mac上的下一个项目示例(asp.net core 1.0): public class BloggingContext : DbContext { public BloggingContext(DbContextOptions options) : base(options) { } public DbSet Blogs { get; set; } public DbSet Posts { get; set; } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List Posts { get; […]

用entity framework执行自定义sql?

我需要执行一个自定义查询,它将被保存在数据库中的某个地方,我需要它返回数据表或数据集并将其绑定到gridview,它将自动生成列为true。 我的所有数据访问层都与entity framework完美配合,但对于某些特定场景,我需要这样做,我想知道我是否应该将ado.net与entity framework结合起来,或者如果EF能够以某种方式实现它

如何在Entity Framework Code First中删除表?

我正在使用Entity Framework和Auto Migrations。 因此,当我向Context添加新模型时,我的数据库会更新并创建新表。 我想要做的是反过来,完全从数据库中删除表。 但是,从Context类中删除定义不起作用。 public class CompanyContext : DbContext { public DbSet Permissions { get; set; } public DbSet Companies { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } } 例如,我想从数据库中删除Company表。 为此,我从CompanyContext类中删除Companies属性。 但是,它不起作用。 如果可能,在EF中删除表并使用自动迁移的正确方法是什么?

使用ASP .NET Core Identity和EntityFrameworkCore注册新用户时出现InvalidOperationException

我正在关注使用Identity的文档 ,我正在尝试注册一个新用户(执行注册操作),但它失败并出现以下错误: InvalidOperationException:无法为“ApplicationUser”创建DbSet,因为此类型未包含在上下文的模型中。 启动: services.AddIdentity(options => { //password options options.Password.RequireDigit = false; // … }) 我使用的是标准的ApplicationUser: public class ApplicationUser : IdentityUser { } 在AccountController中注册操作: public async Task Register(RegisterViewModel viewModel) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = viewModel.UserName, Email = viewModel.Email }; var result = await _userManager.CreateAsync(user, viewModel.Password); //<– Exception happens […]