0

I am not using localdb or ./sqlexpress I have to use server name

MSOLDEV03\SQL2012

Here is my code for generating database; when I run I get a SqlConnection error "server not found":

    public class Blog
    {
        public int BlogId { get; set; }
        public string Name { get; set; }

        public virtual List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }

        public int BlogId { get; set; }
        public virtual Blog Blog { get; set; }
    }

    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }

I used command

 Enable-Migrations

this command makes migration folder but does not generate database on my SQL Server instance MSOLDEV03\SQL2012

Then I tried to run this code

  using (var db = new BloggingContext())
  {
            db.Blogs.Add(new Blog { Name = "Another Blog " });
            db.SaveChanges();

            foreach (var blog in db.Blogs)
            {
                Console.WriteLine(blog.Name);
            }
  }

  Console.WriteLine("Press any key to exit...");
  Console.ReadKey();

Still not generate database but gives error connection to SQL Server not found.

Here is my app.config file contains no sql connection

<entityFramework>
    <defaultConnectionFactory 
          type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameters>
            <parameter value="mssqllocaldb" />
        </parameters>
    </defaultConnectionFactory>
    <providers>
        <provider invariantName="System.Data.SqlClient" 
                  type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>

1 Answer 1

1

In your context class constructor, you can give the key like:

public BloggingContext() : base("name=BloggingContextKey")
        {

        }

and then give this key a value in your configuration file:

<connectionStrings>
    <add name="BloggingContextKey" connectionString="Data Source=<Your server>; Initial Catalog=<Database>;user=<user>;pwd=<password>;" providerName="System.Data.SqlClient" />
  </connectionStrings>
Sign up to request clarification or add additional context in comments.

6 Comments

but Initial Catalog=<Database> i have to create database there is no existing database
Enable-Migrations -ContextTypeName BloggingContextKey -EnableAutomaticMigrations -ConnectionStringName BloggingContextKey but gives error context BloggingContextKey not found
how to give server name in passing parameters in package manager console along with enable migrations\
In code first, first you define classes i.e. your business model. Database will be created automatically when you first time use BloggingContext.
ANSWERS: Initial Catalog=<Database NAME TO CREATE>
|

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.