简体   繁体   English

实体框架 - 混合模型优先和数据库优先方法?

[英]Entity Framework - Mixing model-first and database-first approaches?

For my first project using Entity Framework, I chose to use the "model-first" approach wherein I design my entities from which a script will be created for generating the required database tables. 对于我使用Entity Framework的第一个项目,我选择使用“模型优先”方法,其中我设计我的实体,从中创建脚本以生成所需的数据库表。 This has worked quite well until I ran into a situation where I couldn't quite retrieve all the data elements I am needing in a single query (my LINQ skills are still limited). 这一点非常有效,直到我遇到一个我无法在单个查询中检索所需的所有数据元素的情况(我的LINQ技能仍然有限)。

Since I can easily write the query I need in SQL, I was wondering whether it would be possible to write a view for my database and then generate an entity in my model from it, in other words, mixing the two model/database approaches. 因为我可以轻松地在SQL中编写我需要的查询,所以我想知道是否可以为我的数据库编写一个视图,然后从我的模型中生成一个实体,换句话说,混合两种模型/数据库方法。 Any ideas on this? 有什么想法吗?

你知道SqlQuery()方法吗?

ctx.Listings.SqlQuery("SQL Query Here", p1, p2...)

You can't mix model-first and db-first. 你不能混合模型优先和数据库优先。 Once you manually modify database you can't use Generate database from EDMX any more or you delete your changes performed directly in DB. 手动修改数据库后,无法再使用EDMX中的Generate数据库,或者删除直接在DB中执行的更改。

In some cases it can be avoided by downloading Entity Designer Database Generation Power Pack extension to Visual Studio 2010. When using this extension together with VS 2010 Premium or Ultimate you can use additional DB generation workflows and T4 templates which are able to use VS tools to compare newly generated DB with existing DB and create only ALTER scripts. 在某些情况下,可以通过将实体设计器数据库生成Power Pack扩展下载到Visual Studio 2010来避免这种情况。将此扩展与VS 2010 Premium或Ultimate一起使用时,您可以使用其他数据库生成工作流和能够使用VS工具的T4模板将新生成的DB与现有DB进行比较,并仅创建ALTER脚本。

But still this will most probably not work with DB views because information about DB view is stored in SSDL (storage model description). 但是,这很可能不适用于DB视图,因为有关DB视图的信息存储在SSDL(存储模型描述)中。 Model-first doesn't use Views and every time you regenerate database it creates table instead of view. Model-first不使用Views,每次重新生成数据库时,它都会创建表而不是视图。

So if you want to run arbitrary SQL query use ExecuteStoreQuery (only EF4) or give up with model first. 因此,如果您想运行任意SQL查询,请使用ExecuteStoreQuery (仅限EF4)或先放弃模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架 - 加入方法 - 混合代码优先和数据库优先 - Entity Framework - Joining approaches - Mixing code-first and database first 在数据库优先方法中更新模型实体框架时遇到问题 - having problem with updating model entity framework in database-first approach 具有 SQL 服务器凭据的数据库优先实体框架? - Database-First Entity Framework with SQL Server credentials? 实体框架数据库优先方法 Pascal 案例 - Entity Framework database-first approach Pascal case 具有数据库优先多对多关系问题的实体框架 6 - Entity Framework 6 with Database-First Many to Many relationship problem DbContext无法初始化Model-first数据库 - DbContext fails to initialize Model-first database 具有数据库优先模型的实体框架 - Entity Framework with Database First model 是否可以使用数据库优先的Entity Framework 6更新SQLite数据库文件? - Is it possible to update a SQLite database file using Entity Framework 6 in database-first? 具有输出参数和结果集的实体框架数据库优先方法存储过程 - Entity framework database-first approach stored procedure with output parameter and result set 实体框架数据库优先:从同一个表中访问 2 个外键名称 - Entity Framework database-first: accessing 2 foreign keys names from same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM