简体   繁体   中英

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.

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'. In old 'MyClass' table I have some data. Will the old table 'MyClass' be removed or updated or application will got error?

Update:

I checked table behavior after editing column names:

sql raise error

System.Data.SqlServerCe.SqlCeException (0x80004005): The column name is not valid. [ Node name (if any) = t0,Column name = 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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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