简体   繁体   English

System.Data.Linq.Mapping修改标记为[Table]的类

[英]System.Data.Linq.Mapping Modify class marked as [Table]

I am a beginner with LINQ to SQL and I can't find in internet description of behavior for situation, when class with attribute [Table] was modified. 我是LINQ to SQL的初学者,当属性[Table]的类被修改时,我无法在Internet上找到情况的行为描述。

Example: I have class mapped to table 示例:我将类映射到表

using System;
using System.Data.Linq.Mapping;


namespace MyNamespace
{
    [Table(Name = "MyClass")]
    public sealed class MyClass
    {
        [Column(IsPrimaryKey = true)]
        public Guid Id { get; set; }

        [Column(CanBeNull = false)]
        public decimal TotalSum { get; set; }

        [Column(CanBeNull = false)]
        public decimal ResultSum { get; set; }

        [Column(CanBeNull = false)]
        public MyEnum Status { get; set; }

    }
}

Now i need to remove field 'Status' from class and add field with type 'MyNewEnum'. 现在,我需要从类中删除字段“状态”,并添加类型为“ MyNewEnum”的字段。 In old 'MyClass' table I have some data. 在旧的“ MyClass”表中,我有一些数据。 Will the old table 'MyClass' be removed or updated or application will got error? 旧表“ MyClass”会被删除或更新,还是应用程序出错?

Update: 更新:

I checked table behavior after editing column names: 编辑列名称后,我检查了表的行为:

sql raise error sql引发错误

System.Data.SqlServerCe.SqlCeException (0x80004005): The column name is not valid. System.Data.SqlServerCe.SqlCeException(0x80004005):列名无效。 [ Node name (if any) = t0,Column name = MyNewEnum]. [节点名称(如果有)= t0,列名称= MyNewEnum]。

So can I force drop and recreate table using attributes? 那么我可以使用属性强制删除并重新创建表吗?

for adding column 'MyNewEnum' use below code, whereas removing a column is not available as said here [ https://msdn.microsoft.com/en-us/library/windows/apps/hh394018(v=vs.105).aspx#BKMK_UnusedColumnsTablesandDatabases] 用于添加“ MyNewEnum”列的代码,请使用下面的代码,但是无法删除列,如此处[ https://msdn.microsoft.com/en-us/library/windows/apps/hh394018(v=vs.105)所述。 aspx#BKMK_UnusedColumnsTablesandDatabases]

DatabaseSchemaUpdater dbUpdater = Empdb.CreateDatabaseSchemaUpdater();
// Add the column
try
{
dbUpdater.AddColumn<MyClass>("MyNewEnum"); 
dbUpdater.Execute(); 
}
catch { /* Nothing */ }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM