问题标签 [entity-framework-6.1]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c# - 实体框架和类型动态的属性
我们目前正在从 LinqToSQL 切换到 EF。到目前为止,我们正在使用它DataContext.ExecuteQuery<T>
来运行更复杂的 SQL 语句。有时我们有 T 的类型,其中一些属性是 type dynamic
。
在 LinqToSQL 中,这工作得很好。Entities.Database.SqlQuery<T>
以某种方式返回null
这些属性,尽管它是相同的查询。
是否有可能继续使用模型类而不改变动态属性?
c# - 标记要重新加载的实体集合
我正在使用 EF 6.1 跟踪对从抽象基类继承的所有对象的更改,HistoryData
并将每个对象保存Versions
在父对象的集合中。
它们是这样连接的:
我没有修改条目,而是挂钩DbContext
并执行以下操作:
到目前为止,这工作正常,并且实体被插入而不是更新。
但是,HistoryData
当我调用Foo.Versions
.
所以现在我在结束时做这样的事情InsertInsteadOfUpdate
:
这有效,但它会立即重新加载集合。如果我在查询之前插入几个HistoryData
对象Versions
,这是很多不必要的工作。
我真正想做的就是标记父级的 Collection() 以在我下次查询时重新加载。有没有办法做到这一点?
我试过了
但由于某种原因,这并没有导致 Collection 在下一次调用时延迟加载。
c# - 如何在 EF 6.1 CodeFirst 中的视图上添加导航属性
让我们做一个案例来解释我的问题。
我的表 1
+id
+myTable2Id
我的表2
+id
我的视图1
+id
+myTable2Id
MyView1 存在于案例中,来自MyTable1 的数据。现在我想从我的视图中的 EF6.1 代码优先方法创建一个导航属性到 MyTable2。
我知道数据库优先方法是可能的,但是代码优先方法也可能吗?如何?
编辑:
我在互联网上搜索了一些,但由于 View 这个词的含义很多,很难找到关于它的信息。
同样使用我尝试过的代码中的方法,我总是收到无法完成迁移的错误。因为迁移尝试向视图添加外键,这是不可能的。
编辑2:
详细说明我的解释。我希望能够通过以下方式在代码中处理它:
编辑3:
我会详细说明一下,看看我是否可以更好地解释我的问题。
当我将以下内容添加到我的视图实体中时:
EF 将自动生成以下迁移:
在运行 update-database 时会出现以下错误:
“无法在视图 'MyView1' 上创建索引,因为视图未绑定架构”
编辑4:
在迁移不是石头的评论的帮助下......并且是可变的,我做到了。
我使用了以下fluentAPI:
并将我的表格更改为:(FK 到 MyTable2 并从视图中删除)
我的表 1
+id
我的表2
+id +myTable1
我的视图1
+id
最后哪个更好,因为这样我的模型中的 Null 值更少。
c# - Entity Framework - There is already an object named '[table name]' in the database
I'm getting
There is already an object named '[table name]' in the database.
First I manually created a table in the database. Then I created a code first entity framework that creates the database and the tables if it is not existing. And it suppose to do nothing when it exist.
Initially I uses MigrateDatabaseToLatestVersion to set my DB using entityframework. Using this I'm expecting that it doesn't need to check every table as long as the DB exist. But then I get that error when accessing.
On the otherhand, I try to use other database and doens't create any table and DB. Meaning entityframework will create the DB itself and not manual. After that everything works fine.
Is it because EF should create the table itself? If no, can it be because of the mapping? Could it be also because of DB access? Or could you give me a sample of a flexible set initialize in EF? :)
entity-framework - 在 EF 6 上使用枚举作为 FK
我想在 Code-First 应用程序中使用枚举作为外键。由于枚举存储为 int,我认为我可以在枚举属性上使用属性 [ForeignKey],但它会引发此异常:
这是我正在尝试做的一个例子:
是否可以通过 fluent api 或迁移来执行此操作或类似的操作?
谢谢
entity-framework - EF 6 DbContext Override AsyncSaveChanges
I have a Web API project that uses Entity Framework 6.
Inside my DbContext file I normally override the SaveChanges method in order to do my Audit Log procedure, like this:
Now, I would like to user Async feature in Entity Framework 6, so I need to override the SaveChangesAsync method but can't find any example of how to do that.
Does anyone know how to override the SaveChangesAsync method.?
c# - 更新数据库触发:“数据库中已经有一个名为‘地址’的对象”。
我无法让我的种子方法在已经存在的数据库上运行。该数据库是由多次迁移的结果,我们将自己创建的数据库“Whitelabel”与 ASP.NET MVC5 身份数据库(我们称之为“IdentityDb”)一起迁移。
以下是数据库表的概述:
由于这是代码优先,我使用包管理器控制台使用以下命令更新数据库:
它会触发错误“数据库中已经有一个名为'地址'的对象”。
这是 WhitelabelDb 上下文。
这是 IdentityDb 上下文:
这是 WhitelabelMigrations 的 configuration.cs 文件:
以及使用 System.Data.Entity.Migrations 的 Whitelabel-Migrations 的 InitialCreate.cs 文件;
当然还有 Identity-Migrations InitialCreate.cs
那么,由于我只能从 Whitelabel configuration.cs 文件中访问 Category 类,如何让种子方法运行?
任何解释、想法和建设性帮助将不胜感激!
asp.net-mvc - “ConnectionString 属性尚未初始化。” - 但仅在发布时
我有一个使用 LocalDb 在我的开发机器上完美运行的 MVC5 / EF6.1 网站。
但是,当我将其发布到带有 Azure SQL 数据库的 Azure 网站时,在进行任何数据库交互时都会收到以下错误:
The ConnectionString property has not been initialized.
我已经搜索了所有内容,但找不到在 Azure 上发生这种情况的原因。
堆栈跟踪指向的第一个文件是IdentityModels.cs:45
其中包含以下内容:
当我发布到 Azure 时,我测试了连接字符串(恢复正常),这是设置屏幕:
关于发生了什么的任何想法?
更新:
如果不选择ApplicationDbContext
而是选择DefaultConnection
它可以工作,但是我将无法使用代码优先迁移。我怎样才能ApplicationDBContext
让它再次工作?
javascript - LinQ 生成 statesOfCountry javascript 数组
最后在第一个解决方案中找到如何为 jqGrid 生成相关的过滤器选择......但现在我想知道如何使用 EF 6.1 在我的 MVC 4 应用程序中生成这个 statesOfCountry JavaScript 数组。
我假设 linq 将有助于在我的 MVC 控制器上获得类似的东西:
最后在我的视图上设置我的脚本区域:
它应该是这样还是以其他方式存在?
谢谢!
entity-framework-6.1 - 带有表名的实体框架错误?
我有表Forums和实体Forum。但是当我对此表进行查询时,实体框架生成查询表Fora。在实体类Forum添加属性Table("Forums")我得到正确的查询。为什么会发生这种情况?