简体   繁体   中英

asp.net mvc localdb initialize

How can I initialize a databse? // ASP.NET MVC 3 with msSQL(LocalDb)/V11 I want to code to upload data to the database. ASP.NET MVC3 EF Code First

web.config

<connectionStrings>
    <add name="CompanyContext" connectionString="Data Source=(localdb)\v11.0; Initial Catalog=CompanyContext-20130203175316; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|CompanyContext-20130203175316.mdf"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

Global.asax.cs

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            // Use LocalDB for Entity Framework by default
            Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

Initalize Descriptions of doing this.

namespace tesztfeladat.Models
{
    public class CompanyInitalizer : DropCreateDatabaseIfModelChanges<CompanyContext>
    {
        protected override void Seed(CompanyContext context)
        {
            var contacts = new List<Contact>
            {
                new Contact {
                    Vezetéknév="Nagy",
                    Keresztnév="János",
                    Beosztás="alkalmazott",
                    Email="nagy.janos@default1.com",
                    Telefonszám="06361254452"
                },

                new Contact {
                    Vezetéknév="Kiss",
                    Keresztnév="Ferenc",
                    Beosztás="alkalmazott",
                    Email="kiss.ferenc@default1.com",
                    Telefonszám="06361254452"
                }
            };

            contacts.ForEach(d => context.Contacts.Add(d));
            context.SaveChanges();

            var events = new List<Event>
            {
                new Event {
                    Időpont=DateTime.Parse("12/31/2010"), 
                    Típusa="Tárgyalás",
                    Leírás="Éves költségvetés"
                },

                new Event {
                    Időpont=DateTime.Parse("12/31/2010"), 
                    Típusa="Tárgyalás",
                    Leírás="Éves költségvetés"
                }
            };

            events.ForEach(d => context.Events.Add(d));
            context.SaveChanges();


            var customers = new List<Customer>
            {
                new Customer {
                    Cégnév ="Default1.Kft",
                    Irányítószám= 1012,
                    Város="Budapest",
                    Cím="Tavasz utca 54.",
                    Weblap="http://www.default1.com",
                    Telefonszám="06361254452",
                    Contacts= contacts,
                    Events=events
                },

                new Customer {
                    Cégnév ="Default2.Kft",
                    Irányítószám= 2440,
                    Város="Százhalombatta",
                    Cím="Tél utca 34.",
                    Weblap="http://www.default1.com",
                    Telefonszám="063623254452",
                    Contacts=contacts,
                    Events=events
                }
            };
            customers.ForEach(d => context.Customers.Add(d));
            context.SaveChanges();
        }
    }
}

I tried this,but dont work. :( Help me pls! Thank You!

The database won't initialize until you attempt to access something from the DataContext. Try putting this at the end of the Application_Start method:

using(var context = new CompanyContext())
{
    var temp = context.Contacts.FirstOrDefault();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM