locked
ef code first problem RRS feed

  • Question

  • User1515160841 posted

    Hi.

    i have problem using ef code first with existing database. i have a databse on sql server. db name is deneme.

    <add name="denemeContext" connectionString="data source=WFOR\SQLEXPRESS;Integrated Security=SSPI;initial catalog=deneme;User Instance=true" providerName="System.Data.SqlClient" />

    my connection string in we.config is above. i created classes for my database tables. also i created i class  named denemeContext and i derived it from DbContext. connections, classes in my project is true. i control them many times.

    i run my project but my datas send empty. So, i write a liting query for showing my datas's list. i runs but no record comes. Is there any special option for using codefirst with existing database. Thanks..

    -->

    Tuesday, December 20, 2011 5:30 PM

Answers

  • User1985864055 posted

    Code First can be used with an existing database; see the description at

    http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

    I don't know why it is not working with your connection string, however; could it be your code is trapping an error then not displaying it?  One thing you're missing -- for EF you need to have MultipleActiveResultSets=true in the connection string.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2011 10:46 PM
  • User-1828494216 posted

    This approach is essentially a database first approach, but using code for the mapping instead of a visual designer and XML.

    For me Code First is in cases where you don't already have a db and it is created from Classes with DbInitializer. I just can't imagine writing all the classes by hand when you can create a model in seconds. Even if you don't want to use a Model, you can "steal" classes from it and enhanche them and then delete that model.

    And i agree with Arthur Vickers, what you're saying is as he wrote: essentially a database first approach. When you already have a db, then it's called DB First. And you create classes or model from that exsisting db.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2011 11:30 PM

All replies

  • User-1828494216 posted

    i have problem using ef code first with existing database.

    Is there any special option for using codefirst with existing database.

    First you are saying that you are using Code First (this means that database is generated based on your code) and then you are talking about using exsisting database which is the oposite of code first aproach.

    Please clearify what you are doing here so we can help you.

    Tuesday, December 20, 2011 6:25 PM
  • User1515160841 posted

    i read some articles that are about ef code first with existing database. i ask this. i havea database. i want to add this db to my project but i dont want the codes are auto generating. i want to write entities my own as poco classes. Is this possible?

    Thanks again

     
    Tuesday, December 20, 2011 6:36 PM
  • User-1828494216 posted

    No. Using Code First can not be used with an exsisting DataBase. What you want is simply to use Entity Framework (database first approach) or you can go with LinQ to Sql.

    Building an MVC 3 App with Database First and Entity Framework 4.1

    Tuesday, December 20, 2011 6:42 PM
  • User1985864055 posted

    Code First can be used with an existing database; see the description at

    http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

    I don't know why it is not working with your connection string, however; could it be your code is trapping an error then not displaying it?  One thing you're missing -- for EF you need to have MultipleActiveResultSets=true in the connection string.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2011 10:46 PM
  • User-1828494216 posted

    This approach is essentially a database first approach, but using code for the mapping instead of a visual designer and XML.

    For me Code First is in cases where you don't already have a db and it is created from Classes with DbInitializer. I just can't imagine writing all the classes by hand when you can create a model in seconds. Even if you don't want to use a Model, you can "steal" classes from it and enhanche them and then delete that model.

    And i agree with Arthur Vickers, what you're saying is as he wrote: essentially a database first approach. When you already have a db, then it's called DB First. And you create classes or model from that exsisting db.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2011 11:30 PM
  • User1515160841 posted

    i understood.

    thanks for replies.

    Wednesday, December 21, 2011 1:14 PM