小编48k*_*ocs的帖子

是否可以表达检查约束?

我正在使用Entity Framework 4.3进行代码优先开发,并且看起来似乎不可能通过属性注释或任何其他方式表达CHECK约束.我看到EF 5.0将添加对检查枚举的支持,但这并不是我在这里完成的.

举一个简单的例子,我想验证所有Person对象的名字都是"Bob"或"Harry",并且是5年,10年或30年.

public class Person
{
    [Required]
    [Check("Bob", "Harry")]  //yes, this attribute is imaginary
    public string FirstName { get; set; }

    [Required, Check(5, 30, 50)]  //check is still imaginary
    public int Age { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我可以运行一个alter脚本来添加这些约束,然后我可以滚动自己的check属性来执行验证,但是有没有办法在Entity Framework中实际表达非枚举的CHECK约束?

c# entity-framework entity-framework-4.3

9
推荐指数
2
解决办法
5776
查看次数

如何从给定的父节点获取所有子节点?

我有一个父/子ID列表,并希望获得给定父ID的所有子ID.没有空父项(顶级ID不显示为子ID).

目前,父/子ID在列表中记录为KeyValuePair,但是如果更好的话,可以很容易地将其更改为另一个数据结构:

List<KeyValuePair<int, int>> groups = new List<KeyValuePair<int, int>>();
groups.Add(new KeyValuePair<int,int>(parentID, childID));
Run Code Online (Sandbox Code Playgroud)

例如,以下是示例父/子.父母27的孩子将是5944,2065,2066,2067,6248,6249,6250.

Parent  Child
27      1888
1888    5943
1888    5944
5943    2064
5943    2065
5943    2066
5943    2067
2064    6248
2064    6249
2064    6250
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激!

.net c# algorithm recursion relationship

4
推荐指数
1
解决办法
5020
查看次数

c#反序列化JSON对象错误列表

当我反序列化它的工作列表时,但是当我反序列化为具有列表类型的对象时,它会出错.知道如何让它工作吗?

页面名称:testjson.aspx

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace Web.JSON
{
    public partial class testJson : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string json = "[{\"SequenceNumber\":1,\"FirstName\":\"FN1\",\"LastName\":\"LN1\"},{\"SequenceNumber\":2,\"FirstName\":\"FN2\",\"LastName\":\"LN2\"}]";


            //This work
            IList<Person> persons = new JavaScriptSerializer().Deserialize<IList<Person>>(json);

            //This error
            //People persons = new JavaScriptSerializer().Deserialize<People>(json);


            Response.Write(persons.Count());
        }
    }

    class Person
    {
        public int SequenceNumber { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    class People : List<Person>
    {
        public People()
        {

        } …
Run Code Online (Sandbox Code Playgroud)

c# serialization json javascriptserializer

1
推荐指数
1
解决办法
3679
查看次数