简体   繁体   English

是否可以使用实体框架并将对象关系保留在代码中以及数据库之外

[英]Is it possible to use Entity Framework and keep object relations in the code and out of the database

I'm having a hard time just defining my situation so please be patient. 我很难定义自己的处境,所以请耐心等待。 Either I have a situation that no one blogs about, or I've created a problem in my mind by lack of understanding the concepts. 我遇到的情况是没人写博客,或者由于缺乏理解这些概念而在脑海中制造了一个问题。

I have a database which is something of a mess and the DB owner wants to keep it that way. 我有一个混乱的数据库,数据库所有者希望保持这种状态。 By mess I mean it is not normalized and no relationships defined although they do exist... 乱七八糟的意思是,虽然它们确实存在,但尚未规范化,也未定义任何关系...

I want to use EF, and I want to optimize my code by reducing database calls. 我想使用EF,并且想通过减少数据库调用来优化代码。

As a simplified example I have two tables with no relationships set like so: 作为一个简化的示例,我有两个表,它们没有像这样设置关系:

Table: Human HumanId, HumanName, FavoriteFoodId, LeastFavoriteFoodId, LastFoodEatenId 表:人类HumanId,HumanName,FavoriteFoodId,LeastFavoriteFoodId,LastFoodEatenId

Table: Food FoodId, FoodName, FoodProperty1, FoodProperty2 表格:食品FoodId,FoodName,FoodProperty1,FoodProperty2

I want to write a single EF database call that will return a human and a full object for each related food item. 我想编写一个EF数据库调用,它将为每个相关食品返回一个人类和一个完整的对象。

First, is it possible to do this? 首先,可以这样做吗?

Second, how? 第二,如何?

Boring background information: A super sql developer has written a query that returns 21 tables in 20 milliseconds which contain a total of 1401 columns. 无聊的背景信息:一位超级sql开发人员编写了一个查询,该查询在20毫秒内返回21个表,其中总共包含1401列。 This is being turned into an xml document for our front end developer to bind to. 这正在变成一个XML文档,供我们的前端开发人员绑定。 I want to change our technique to use objects and thus reduce the amount of hand coding and mapping from fields to xml (not to mention the handling of nulls vs empty strings etc) and create a type safe compile time environment. 我想改变我们的技术来使用对象,从而减少从字段到xml的手工编码和映射的数量(更不用说处理空值与空字符串等了),并创建一个类型安全的编译时环境。 Unfortunately we are not allowed to change the database or add relationships... 不幸的是,我们不允许更改数据库或添加关系。

If I understand you correct, it's better for you to use Entity Framework Code First Approach : 如果我理解您是正确的,那么最好使用Entity Framework Code First方法

  1. You can define your objects (entities) Human and Food 您可以定义您的对象(实体)人类和食物
  2. Make relations between them in code even if they don't have foreign keys in DB 即使它们在数据库中没有外键,也可以在代码之间建立关系
  3. Query them usinq linq-to-sql 查询他们usinq linq-to-sql

And yes, you can select all related information in one call. 是的,您可以在一个呼叫中选择所有相关信息。

You can define the relationships in the code with Entity Framework using Fluent API . 您可以使用Fluent API在带有Entity Framework的代码中定义关系。 In your case you might be able to define your entities manually, or use a tool to reverse engineer your EF model from an existing database. 在您的情况下,您可能可以手动定义实体,或使用工具从现有数据库对EF模型进行反向工程。 There is some support for this built in to Visual Studio, and there are VS extensions like EF Power Tools that offer this capability. Visual Studio内置对此有一些支持,并且有EF扩展之类的VS扩展提供了此功能。

As for making a single call to the database with EF, you would probably need to create a stored procedure or a view that returns all of the information you need. 至于使用EF一次调用数据库,您可能需要创建一个存储过程或一个返回所需信息的视图。 Using the standard setup with lazy-loading enabled, EF will make calls to the database and populate the data as needed. 使用启用了延迟加载的标准设置,EF将调用数据库并根据需要填充数据。

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

相关问题 具有关系的实体框架代码优先数据库初始化 - Entity Framework Code First Database Initialization with relations 是否可以对未知数据库表使用实体框架? - Is it possible to use Entity Framework for unknown database tables? 首先在实体框架代码中初始化数据库中的对象 - Initialize object in database in Entity Framework Code First 首先使用实体​​框架代码与nosql数据库 - Use Entity framework code first with nosql database 具有关系的实体框架6代码优先更新行 - Entity Framework 6 Code First Update Row with Relations 实体框架6:克隆对象和关系(ID除外) - Entity Framework 6: Clone object and relations except ID 实体框架到同一列的多个对象关系 - Entity Framework multiple object relations to same column 如果我们使用具有代码优先方法的Entity框架,是否可以在给定路径上创建数据库(Sql Server compact)? - Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach? 是否可以将数据库优先模型和代码优先模型与实体框架混合使用? - Is it possible to mix database first and code first models with entity framework? 将 .net 4.0 中的实体框架与 Oracle 数据库一起使用 - 可能吗? - Use Entity Framework in .net 4.0 with Oracle database - possible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM