【问题标题】:Working with two different tables with same structure使用具有相同结构的两个不同表
【发布时间】:2012-02-01 18:54:45
【问题描述】:

我有以下问题: 在我的 WP7 项目中,我在 SQL-CE 数据库中有两个结构相同的表。 我想根据某些条件动态使用这些表之一。例如:

[Table]
public class myTable1 : someStructure { }

[Table]
public class myTable2 : someStructure { }

[Table]
public class someStructure
{
    [Column (IsPrimaryKey = true, IsDbGenerated = true)]
    public Int32 ID { get; set; }

    [Column]
    public String Name { get; set; }
}

public class myDB : DataContext
{
    public myDB() : base("Data Source=isostore:/main.sdf") { }

    public Table<myTable1> myTable1;
    public Table<myTable2> myTable2;
}

public partial class MainPage : PhoneApplicationPage
{
    private void doit_Click(object sender, RoutedEventArgs e)
    {
        var _myDB = new myDB();
        if (!_myDB.DatabaseExists())
            _myDB.CreateDatabase();

        Table<someStructure> _table;
        if ( SOMECONDITION )
            _table = _myDB.myTable1;
        else
            _table = _myDB.myTable2;

        _table.InsertOnSubmit(new someStructure { Name = "aaa" });

        _myDB.SubmitChanges();
    }
}

我在_myDB.CreateDatabase() 行收到Unable to create database because mapped class 'databasetest1.myTable1' has zero members. 错误,并且someStructure 类型的警告无法转换为myTable1 和/或myTable2 类型。

我应该怎么做才能解决这个任务?

提前致谢。

【问题讨论】:

    标签: c# windows-phone-7 sql-server-ce


    【解决方案1】:

    就我所尝试的而言,虽然我没有直接从 CE 人员那里得到它,但我从来没有能够让 [Table] 拥有一个带有任何 [Column] 属性的基类。您可以考虑使用 T4 模板通过代码生成您需要的位来为您解决这个问题。

    看看这个,看看它是否有帮助。我自己没有尝试过,但看起来很有希望......

    http://blogs.msdn.com/b/writingdata_services/archive/2012/01/25/new-and-improved-t4-template-for-odata-client-and-local-database.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-25
      • 2019-10-14
      • 1970-01-01
      • 1970-01-01
      • 2018-03-23
      • 2015-03-21
      • 2019-07-14
      相关资源
      最近更新 更多