表映射问题 [英] Table Mapping problem

查看:117
本文介绍了表映射问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在当前项目中使用实体框架4,以从多个表中读取数据. 与使用ADO.net进行比较,它的EF非常简单,只需简单的代码即可完成很多工作或完成我的工作.

但是有一个问题...

e,g存在并且正在退出表调用表"MTable" 我只想查询该表中的两列,但是此表与其他两个正在处理它的人共享.他们可能会在此表上添加列或修改约束. 我唯一确定的是,我要查询的两列内容不会删除或重命名.

我的应用程序现在正在运行,但是有时会中断,因为我是从数据库架构生成代码的,每次有人对"MTable"进行一些更新时,我都需要更新我的应用程序的映射.

有没有办法进行代码拳头"映射,允许我编写一个简单的模式来映射到"MTable",并且仅映射两列,这样我就可以不管其他ppl在"MTable"上做了什么"???

谢谢

解决方案

您的问题非常不清楚.您说的是从架构中生成代码,同时又在问是否有一种方法可以首先使用代码映射代码.

DbContext API!=代码优先方法.人们应该在Fluent/Annotations映射和从代码生成数据库的代码优先方法之间有所区别.如果您从数据库生成代码,则显然是在使用数据库优先方法.

数据库优先的解决方案:数据库视图,但它将使您的实体变为只读.恕我直言,即使表发生了变化,您也不应该有问题.如果只需要两列,而没有人会更改这两列或创建新的必需列,则映射的实体仍然应该起作用.如果有人更改您的专栏,将没有有效的方法来避免破坏您的代码.

您还可以使用EDView的高级功能,例如QueryView(在映射描述中查看)和DefiningQuery(在存储描述中自定义选择),但对于您的方案而言,这些功能可能会显得过时.

代码优先的解决方案:关闭模型元数据验证和数据库初始化.它需要将初始化程序设置为null:

// Use this code in the application start up
Database.SetInitializer<MyContext>(null);

并删除IncludeMetadataConvetion:

public class MyContext : DbContext
{
    ...

    protedte override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<IncludeMetadataConvention>();   
    }
}

在这两种方法中,您还可以使用自定义SQL查询或自定义linq投影.

I am using entity framework 4 in my current project, to read data from several table. Compare using ADO.net, it EF is very easy, with simple code that can do lots of work or me.

But there is one problem...

e,g there is and exiting table call Table "MTable" i only want to query two column from this table, however this table is share with other two ppl who are also working on it. they might add column or modify constrains on this table. Only one thing i am sure is that, the two column i want to query they wont delete it or rename it.

My application is runing now, but from time to time it break because the i generate code from database schema, every time some one do some update to "MTable", i need to update the mapping of my application.

Is there a way to do "Code-fist" maping, allow me to write a simple mode to map to "MTable", and only map the two column, so that i can regardless what what other ppl do on "MTable"???

Thanks

解决方案

Your question is very unclear. You are saying that you are generating the code from your schema and in the same time you are asking if there is a way to map it with code first.

DbContext API != the code first approach. People should differ between Fluent / Annotations mapping and the code-first approach where the database is generated from the code. If you generate the code from the database you are obviously using the database first approach.

Solution for the database-first: Database view but it will make your entity readonly. Imho you should not have problems even if the table changes. If you need just two columns and nobody will change these two columns or create new required columns your mapped enity should still work. If somebody changes your column there will be no effective way to avoid breaking your code.

You can also use advanced EDMX features like QueryView (view in mapping description) and DefiningQuery (custom select in storage description) but those are probably overkill for your scenario.

Solution for the code-first: Turn off model metadata validation and database initialization. It requires setting initializer to null:

// Use this code in the application start up
Database.SetInitializer<MyContext>(null);

and removing IncludeMetadataConvetion:

public class MyContext : DbContext
{
    ...

    protedte override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<IncludeMetadataConvention>();   
    }
}

In both approaches you can also use custom SQL query or custom linq projection.

这篇关于表映射问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆