在Win7机器上使用C#我需要在实现EF之前动态更改Entity Framework 6.2 edmx xml文件中的模式类型。我可以为表的“Schema”属性过滤edmx Xml,但不能过滤视图。表使用非前缀属性“Schema”,而Views因某些奇怪的原因使用前缀属性“store:Schema”。
不幸的是,如果我为明显的“store:Schema”过滤属性,我会得到一个XmlException,说“''''字符,十六进制值0x3A,不能包含在名称中。”我已经尝试了各种方法来过滤这个所需的属性,以便我可以将属性更改为我的自定义模式名称,以便我以后可以让EF运行来自另一个模式的视图而不是“dbo”。我的XML代码如下。如何过滤前缀属性的存储后代?
// How can I specify the schema prefix for an EF edmx <EntitySet> view type?
// Table <EntitySet> has a non prefixed attribute call "Schema"
// EF view <EntitySet> has a prefixed attribute called "store:Schema" that I cannot seem to filter for
// Filtering with "store:Schema" throws an XmlException
// - "The ':' character, hexadecimal value 0x3A, cannot be included in a name."
//
// Change the schema for all the tables and views from the dbo (or whatever) EF specified schema (if any)
// to the desired schema.
foreach (var entitySet in storageXml.Descendants(storageNS + "EntitySet"))
{
// this works fine for tables but views have prefixed attribute "store:Schema" so are not found
var schemaAttribute = entitySet.Attributes("Schema").FirstOrDefault();
if (schemaAttribute != null)
{
schemaAttribute.SetValue(schema);
}
// EF storage xml species a prefix on view Schema attribute as "store:Schema" so are not found
// here we try various ways to get any "store:Scehma" attributes so far unsuccessfully
// the most obvious incantaion throws an XmlException as mentioned above
schemaAttribute = entitySet.Attributes("store:Schema").FirstOrDefault();
// the next three run fine but do not filter out the needed attribute
//schemaAttribute = entitySet.Attributes(XName.Get("store", "Schema")).FirstOrDefault();
//schemaAttribute = entitySet.Attributes(XName.Get("Schema", "store")).FirstOrDefault();
//XNamespace ns = "store";
//schemaAttribute = entitySet.Attributes(ns + "Schema").FirstOrDefault();
if (schemaAttribute != null)
{
// I never hit this though can see the desired XElement w/ a "store:Schema" attribute
// I need to reset this schema // does it need to be set as store: prefixed?
schemaAttribute.SetValue(schema);
}
}
答案 0 :(得分:1)
更改代码以使用像这样的NameSpace前缀。
XNamespace store = "http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator";
schemaAttribute = entitySet.Attributes(store + "Schema").FirstOrDefault();
if (schemaAttribute != null)
{
schemaAttribute.SetValue(schema);
}