Entity Framework

Options
Kreag
edited February 2012 in Photon Server
I've noticed some posts suggesting to use Entity Framework against a DB for persistence.
Has anyone actually used it? Where does the connection string go since there is no app/web.config file?

I have a feeling it would go in the context constructor since I'm using code first.. but that is just guessing.
Also, is anyone using sqlexpress during dev.. then switching to another DB (full sql or mysql) when going live?

Thanks all!

Comments

  • Oh.. one more thing.. where do you put the db initializer stuff if you don't have a global.asax? (Is it obvious I work in aspnet/MVC normally?)

    I'm thinking using code first EF might have been a bad decision :)
  • A Follow-up:
    I added the connectionstring to the contexts' constructor and got this message in the log when I try to use the context:

    2012-01-27 20:39:12,980 [32] DEBUG SpaceServer.UnityClient [(null)] - Received login request from: Kreag
    2012-01-27 20:39:13,000 [32] DEBUG PhotonHostRuntime.PhotonDomainManager [(null)] - AssemblyLoadEvent: System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

    Now why would it use an oracle client when I use this connection string?

    Data Source=CRAIGHP/SQLEXPRESS;Initial Catalog=SpaceCommon.SpaceContext;Integrated Security=True;Provider=SQLNCLI10
  • Well.. for those that might be interested, my solution was to specify the connection string after instantiation.
    Like this:
    db.Database.Connection.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=SpaceDB;Integrated Security=True";

    Once I did this, photon cranked up the correct db provider and connected to the right db. Yay! Code first is and will work! :)
    I may want to note that I also have a MVC web project using this db for user management etc, which is why I want to use "code first".

    Isn't game development maddening and exciting all at the same time?
  • dreamora
    Options
    hehe yeah it is an interesting field isn't it ;)
    And its not photons behavior here, but .NET itself ;)

    Its interesting though that you mention 'a webproject that will also use the db too' and at the same time use sql express which is more targeted at embedded dbs due to its killing limitations (single cpu, 1gb of ram - thats not able to handle any realistic load, less capable than even sqlite which is targeted at embedded only usage) which easily and fast kills it for game usage unless you expect your game to be totally unsuccessful running low user and CCU numbers and only a limited amount of sql queries / s so the lack of data cache is no problem.
  • Oh yes, SQL Express is only being used for running on my dev machine. When I roll this out even for in house testing, we'll be using a true SQL Server (or a free alternative). The cool part is that when I do roll it out, I can add a connection string to my web.config that will point to the real DB server and when I run the app, the DB (since I'm using EF4.2 code first) will be automatically created for me with tables, seed data, relationships etc intact :)
  • dreamora
    Options
    True :)
    But only if you roll to MSSQL (as SQLExpress is the little brother) otherwise you are gonna rewrite the DB code and queries for the new SQL server as each acts differently, has different limitations on indexes and procedures etc
  • FYI: EF is ignoring the connection string and connecting the the "convention over configuration" DB. I'm not sure why yet.
    I'll post the solution here unless anyone has any idea?

    ie; my connection string is "Data Source=CraigHP\\SqlExpress;Initial Catalog=Space;Integrated Security=True"
    .. but the framework is trying to connect to "SpaceWeb.Models.SpaceContext", which is what it should connect to if there was no connection string.
  • Here is the answer.. verified better this time :)

    I was right, you have to pass the connectionstring to the constructor, but pass it to the base constructor of the context like so:
    public SpaceContext()
                : base("Data Source=CraigHP\\SqlExpress;Initial Catalog=Space;Integrated Security=True")
            {
                
            }