简体   繁体   中英

Change column name in asp.net identity

I've declared a class to create new table Topics :

Table["Topics"]
public class TopicViewModels
{
    [Key]
    public string Id { get; set; }
    public string UserId { get; set; }
    public string Content { get; set; }
    public DateTime DateTime { get; set; }
}

I update the database successful and add some rows to the table. Now, I want to change column Id to TopicId without losing data, how can I do that?

I've tried: Right click table Topics , then click Open Table Definition , change the name Id to TopicId and update it.

That's okay. But the problem is: how to rename property Id in class TopicViewModels to TopicId ?

using (var db = new MyDbContext())
{
    string id = "";
    var topic = db.Topics.SingleOrDefault(x => x.Id == id);

    // what I want to achieve:

    string topicId = "";
    var _topic = db.Topics.SingleOrDefault(x => x.TopicId == topicId);

}

If I change to:

Table["Topics"]
public class TopicViewModels
{
    [Key]
    public string TopicId { get; set; }
    public string UserId { get; set; }
    public string Content { get; set; }
    public DateTime DateTime { get; set; }
}

the data would be lost. That's similar to: delete column Id and add new column TopicId .

So, my question: how can I rename some property in class TopicViewModels without losing data?

使用ColumnAttribute [Column("Id")]

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