0

I have restricted access to SQL server database & I have to alter table from my MVC project. I have tried:

var db = MyDbContext.Create();
try
{
    var res =
    db.Speakers.SqlQuery("ALTER TABLE [dbo].[Speakers] ADD [LastName] [nvarchar](256) NULL");
}
catch (Exception exception)
{
    Console.WriteLine(exception);
}

its not throwing exception but not updating table. I have no idea whether Raw query allow alteration or not. But I just gave a try. Can any one tell how can I alter database?

2 Answers 2

1

Try this approach:

    db.Database
.ExecuteSqlCommand("ALTER TABLE [dbo].[Speakers] ADD [LastName] [nvarchar](256) NULL");
Sign up to request clarification or add additional context in comments.

Comments

1

Non-query commands can be sent to the database using the ExecuteSqlCommand method on Database. For example:

using (var context = new BloggingContext()) 
{ 
    context.Database.ExecuteSqlCommand( 
        "UPDATE dbo.Blogs SET Name = 'Another Name' WHERE BlogId = 1"); 
}

Note that any changes made to data in the database using ExecuteSqlCommand are opaque to the context until entities are loaded or reloaded from the database.

Entity Framework Raw SQL Queries https://msdn.microsoft.com/en-gb/data/jj592907.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.