简体   繁体   English

如何从Entity Framework 4.0对象获取架构名称?

[英]How to get Schema name from Entity Framework 4.0 objects?

I'm creating some manual SQL updates using C#, Entity Framework 4 and DB2, in the way of this naive example... 通过这个简单的示例,我正在使用C#,Entity Framework 4和DB2创建一些手动SQL更新。

var UpdateCommand = "UPDATE MY_SCHEMA." + this.Entities.PRODUCT.EntitySet.Name +
                    " SET STOCK=0";
var AffectedRows = this.Entities.ExeceuteStoreCommand(UpdateCommand);

I want to specify the schema as with the entity name (which later, if implememented in a reusable library method, could be passed as parameter). 我想将实体名称指定为架构(如果以后在可重用的库方法中实现,则可以将其作为参数传递)。 So, I tried... 所以,我尝试了...

var Container = this.Entities.MetadataWorkspace.GetEntityContainer(this.Entities.DefaultContainerName, System.Data.Metadata.Edm.DataSpace.CSpace);
var Set = Container.GetEntitySetByName(this.Entities.PRODUCT.EntitySet.Name, true);
var SchemaName = Set.MetadataProperties["Schema"].Value;

The problem is that the SchemaName returned is always null! 问题在于返回的SchemaName始终为null!

I've seen solutions based on parsing SQL generated by Entity Framework, but that could be fooled (text can containg anything), or SQL-Server specific. 我已经看到了基于解析由Entity Framework生成的SQL的解决方案,但是这可能会被愚弄(文本可以包含任何内容)或特定于SQL Server。 The idea is to be as DB agnostic as EF is. 这个想法与EF一样,与数据库无关。

Question is... how to get an entity schema name from EF objects, not parsing generated SQL and being db-agnostic? 问题是...如何从EF对象获取实体架构名称,而不是解析生成的SQL并与数据库无关?

you can get it from SSpace. 您可以从SSpace获得它。

In ef5 following works. 在ef5中,以下作品。 Probably will work in ef4 also 可能也会在ef4中工作

// for code-first
var Container = this.Entities.MetadataWorkspace.GetEntityContainer("CodeFirstDatabase", DataSpace.SSpace);
// db-first
var Container = this.Entities.MetadataWorkspace.GetEntityContainer("DbFirstModelStoreContainer", DataSpace.SSpace);

var schemaName = Container.GetEntitySetByName(this.Entities.PRODUCT.EntitySet.Name, true).Schema
// or
var set = Container.GetEntitySetByName(this.Entities.PRODUCT.EntitySet.Name, true);
var schemaName = Set.MetadataProperties["Schema"].Value;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM