如何确保EntityRef不能为空?

时间:2012-02-18 17:19:56

标签: linq-to-sql windows-phone-7

我正在尝试建立一个简单的一对多关系,其中列表可以包含零个或多个任务,并且任务必须具有关联的列表。我想通过单元测试来确保这种行为,但我无法弄清楚关联是如何进行的,因此测试将失败(稍后我将使用ExpectedExceptionAttribute,但我甚至不知道将抛出哪个异常尚未)。

任务可以成功提交,但我希望看到Linq抱怨缺少List参考:

[TestMethod]
public void SavingTaskWithoutListFails()
{
    var task = new Task() { Title = "Test Task", Description = "This is the task description." };
    Db.Tasks.InsertOnSubmit(task);
    Db.SubmitChanges();            
}

我的课程和协会如下所示:

[Table]
public class Task
{
    [Column(AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)]
    public int TaskId { get; set; }

    [Column(IsPrimaryKey = true)]
    public string Title { get; set; }

    [Column]
    public string Description { get; set; }

    [Column]
    public int ListId { get; set; }

    private EntityRef<List> _list;

    [Association(Storage = "_list", ThisKey = "ListId")]
    public List List
    {
        get { return _list.Entity; }
        set { _list.Entity = value; }
    }
}

和此:

[Table]
public class List
{
    [Column(AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)]
    public int ListId { get; set; }

    [Column(IsPrimaryKey = true)]
    public string Title { get; set; }

    private EntitySet<Task> _tasks;

    [Association(Storage = "_tasks", OtherKey = "TaskId")]
    public EntitySet<Task> Tasks
    {
        get { return _tasks; }
        set
        {
            if (_tasks == null)
            {
                _tasks = new EntitySet<Task>();
            }
            _tasks.Assign(value);
        }
    }
}

任何人都可以帮助正确关联或提示如何实现所需的行为吗?

1 个答案:

答案 0 :(得分:0)

您应该使用[Column(CanBeNull = false)]来获得关联列。

编辑:

[Table]
public class Task
{
    [Column(IsPrimaryKey = true, AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)]
    public int TaskId { get; set; }

    [Column]
    public string Title { get; set; }

    [Column]
    public string Description { get; set; }

    [Column(CanBeNull =  false)]
    public int ListId { get; set; }

    private EntityRef<List> _list;

    [Association(Storage = "_list", ThisKey = "ListId", IsForeignKey = true, OtherKey = "ListId")]
    public List List
    {
        get { return _list.Entity; }
        set { _list.Entity = value; }
    }
}

[Table]
public class List
{
    [Column(IsPrimaryKey = true, AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsDbGenerated = true)]
    public int ListId { get; set; }

    [Column]
    public string Title { get; set; }

    private EntitySet<Task> _tasks;

    [Association(Storage = "_tasks", OtherKey = "TaskId", ThisKey = "ListId")]
    public EntitySet<Task> Tasks
    {
        get { return _tasks; }
        set
        {
            if (_tasks == null)
            {
                _tasks = new EntitySet<Task>();
            }
            _tasks.Assign(value);
        }
    }
}

我将两个表的IsPrimaryKey移至Id而不是Title,将关联的一侧设为IsForeignKey=true并填充ThisKey和{{ 1}}用于结合的双方。当我尝试添加没有ListId值的新任务时,SubmitChanges抛出了SlqCEException,并显示以下消息:OtherKey