0

我正在使用 C# ASP.NET MVC 进行企业 Web 开发项目,他们的一个要求是具有动态属性以及实体之间的动态关系。因此,他们希望能够更改属性/关系,而无需经历更改 SQL 模式/ORM 类的麻烦。我们正在使用 LINQ-to-SQL。

所以我的问题是;实现它的最佳方法是什么?

4

1 回答 1

1
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.
于 2012-08-21T23:42:14.837 回答