DethoRhyne
DethoRhyne

Reputation: 980

Configuring new table to EntityFramework(Core) in .NET Core

I have added a new table to the localDB called ChatLog where I store chat messages and I have created 3 stored procedures for the table.

I've been looking online on how to configure that table for Entity Framework but I can't find a solution that works for me, I've even tried the override work arounds but usually there's an error revolving around DbContext that's not pulled from services.

I'm new at this and don't really know what I should do, and as I said, tutorials arent really helping since I lack the knowledge.

could some one give me a hand here? the table looks like this:

Column      | type
----------------------------
SenderId    | nvarchar(256)
RecipientId | nvarchar(256)
Message     | ntext
Time        | date

I have tried writing this, but it doesn't work:

[Table("ChatLog")]
public class ChatMessage
{
    [Required]
    [Column("SenderId")]
    public string SenderId { get; set; }
    [Required]
    [Column("RecipientId")]
    public string RecipientId { get; set; }
    [Required]
    [Column("Message")]
    public string Message { get; set; }
    [Required]
    [Column("Time")]
    public DateTime Time { get; set; }
}

class ChatLogContext : DbContext
{

    public DbSet<ChatMessage> ChatMessages { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ChatMessage>()
            .ToTable("ChatLog");
    }
}

Upvotes: 0

Views: 328

Answers (1)

Sunil Kumar
Sunil Kumar

Reputation: 3242

You have to update your .edmx file for apply database changes, Please follow the below steps to update your table :

To update the .edmx file when the database changes :

1) Open your .edmx file by simply clicking on.

2) When the .edmx file is opened then simply right click on select Update Model from Database.

The Update Model Wizard starts. If there is no database connection specified, the Choose Your Database Connection dialog box appears. Otherwise, the Choose Your Database Objects dialog box appears.

3) If the Choose Your Database Connection dialog box appears, specify a database connection. Otherwise, go to the next step.

4) Click the Add tab.

5) Expand the Tables, Views, and Stored Procedures nodes, and check the objects you want to add to the .edmx file.

6) Click Finish to update the .edmx file with the database changes.

after applying this you will get your desired Table in your .edmx file.

Hope it helps you.

Happy Coding :)

Upvotes: 1

Related Questions