C# 创建动态属性/关系
我正在使用C ASP.NET MVC进行一个企业web开发项目,其中一个要求是具有动态属性以及实体之间的动态关系。因此,他们希望能够更改属性/关系,而不必经历更改SQL模式/ORM类的麻烦。我们正在使用LINQtoSQLC# 创建动态属性/关系,c#,asp.net,asp.net-mvc,linq-to-sql,C#,Asp.net,Asp.net Mvc,Linq To Sql,我正在使用C ASP.NET MVC进行一个企业web开发项目,其中一个要求是具有动态属性以及实体之间的动态关系。因此,他们希望能够更改属性/关系,而不必经历更改SQL模式/ORM类的麻烦。我们正在使用LINQtoSQL 所以我的问题是,;实现这一点的最佳方法是什么?哇,对不起,我的愚蠢,我指的是动态属性!后期编辑。不愚蠢,这是我的假设。考虑到这一点,这确实是一个合适的包类型模式。谢谢你的代码,伙计!但有一个问题,字典是如何存储在数据库中的?如何在DB中创建字段?这是您只需要实体属性表的地方,它
所以我的问题是,;实现这一点的最佳方法是什么?哇,对不起,我的愚蠢,我指的是动态属性!后期编辑。不愚蠢,这是我的假设。考虑到这一点,这确实是一个合适的包类型模式。谢谢你的代码,伙计!但有一个问题,字典是如何存储在数据库中的?如何在DB中创建字段?这是您只需要实体属性表的地方,它需要有实体表的外键,并且您只需像其他主细节一样在查询中加入它。问题在于数据类型。您可能可以在您的ORM映射中做到这一点,但不太熟悉linq到SQLOkay。但是问题是管理员选择了属性,这是他的用户的用户配置文件。然后用户可以填写这些详细信息。因此,UserProfile表具有一对多关系的UserProfileProperties表的外键。好吧,我明白了,但当用户填写这些详细信息时,我不知道该从哪里开始。如何存储拾取的数据属性值?JSON?如果某个用户填写了旧的属性,然后管理员对其进行了更改,该怎么办?
void Main()
{
var db = @"<root>
<entities>
<entity id='1' name='tim'/>
<entity id='2' name='Gaui'/>
</entities>
<properties>
<property id='1' entityid='1' name='Age' value='46'/>
<property id='2' entityid='1' name='Country' value='Australia'/>
<property id='3' entityid='2' name='StackoverflowRep' value='17'/>
</properties>
</root>";
var doc = XElement.Parse(db);
var ents = from e in doc.Descendants("entity")
select new Entity()
{
id = (int)e.Attribute("id"),
name = (string)e.Attribute("name"),
Properties = doc.Descendants("property")
.Where(p => (int)p.Attribute("entityid") == (int)e.Attribute("id"))
.Select( p => new { name = (string)p.Attribute("name") , value = (string)p.Attribute("value")})
.ToDictionary(k => k.name, v => v.value)
};
ents.Dump();
}
public class Entity
{
public int id {get;set;}
public string name {get;set;}
public IDictionary<string, string> Properties {get;set;}
}
// You can run this spike in LinqPad (language c# Program)
// In this example I just have a simple master detail, and the assumtion is that the name/value pairs are strings.
// In a real app you probably would have another table with property types, something like...
//<propertytypes>
// <propertytype id='1' Name='Country' type='string'>
// ....etc
// </propertytype>
//</propertytypes>
// and in the properties table you would join to this instead.
// This would give you the data types, plus the ability to
// populate a list of allowed properties etc.