将自定义对象列表存储在隔离存储中时出错

时间:2014-10-28 10:10:07

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

我有以下类型的自定义类

[Table]
class MyApp
{
    public MyApp()
        : base()
    {

    }

    [Column(IsPrimaryKey=true, UpdateCheck = UpdateCheck.Never)]
    public string appCode { get; set; }

    [Column(UpdateCheck = UpdateCheck.Never)]
    public string procCode { get; set; }
}

我有另一个包含MyApp对象列表的类,如下所示:

[Table]
class ApplicationUser
{
    public ApplicationUser() 
        :base()
    {

    }

    [Column(IsPrimaryKey = true, UpdateCheck = UpdateCheck.Never)]
    public string userId { get; set; }

    [Column(UpdateCheck = UpdateCheck.Never)]
    public List<MyApp> applicationList { get; set; }
}

在我的DataContext类中调用CreateDatabase()方法时,出现以下错误:

Unable to determine SQL type for 'System.Collections.Generic.List`1[XCell.Framework.data.MyApp]'

请指导我。

1 个答案:

答案 0 :(得分:1)

我发现问题是applicationList标有Column属性,但它代表了一种关系。

基本上,您必须使用EntityRef<T>EntitySet<T>类以及Association属性正确映射这些实体之间的关系。

This article可能会有所帮助。

下面更正的映射(对于一对多关系)的示例:

调整后的ApplicationUser班级

[Table]
public class ApplicationUser
{
    private EntitySet<MyApp> _userApplications = new EntitySet<MyApp>();

    [Column(IsPrimaryKey = true, UpdateCheck = UpdateCheck.Never)]
    public string UserId { get; set; }

    [Association(Storage = "_userApplications", ThisKey = "UserId", OtherKey = "ApplicationUserId")]
    public EntitySet<MyApp> ApplicationList
    {
        get { return _userApplications; }
        set { _userApplications = value; }
    }
}

经过调整的MyApp班级

[Table]
public class MyApp
{
    private EntityRef<ApplicationUser> _applicationUserRef;

    [Column(IsPrimaryKey = true, UpdateCheck = UpdateCheck.Never)]
    public string AppCode { get; set; }

    [Column(UpdateCheck = UpdateCheck.Never)]
    public string ProcCode { get; set; }

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

    [Association(Name = "FK_MyApp_ApplicationUser", Storage = "_applicationUserRef", ThisKey = "ApplicationUserId", OtherKey = "UserId")]
    public ApplicationUser ApplicationUserReference
    {
        get { return _applicationUserRef.Entity; }
        set { _applicationUserRef.Entity = value; }
    }
}