Domanda

Sono completamente nuovo nell'uso del codice prima per la modellazione di database. Ho provato a seguire diversi tutorial e mi sono imbattuto in tutti, e non riesco mai a capire perché.

In questo momento ho un nuovo progetto MVC 4. Sto lavorando a questo progetto con altri 3 e stiamo utilizzando Team Foundation Server per il controllo di origine. Seguendo vari tutorial, ho impostato i miei modelli così:

public class User
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }

    public virtual ICollection<Entry> Entries { get; set; }
    public virtual ICollection<Rating> Ratings { get; set; }
}

public class Contest
{
    public int ContestId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
    public Boolean Published { get; set; }

    public virtual ICollection<Submission> Submissions { get; set; }
    public virtual ICollection<Tab> Tabs { get; set; }
}

public class Tab
{
    public int TabId { get; set; }
    public string Name { get; set; }
    public int Order { get; set; }
    public string Content { get; set; }
    public int ContestId { get; set; }

public virtual Contest Contest { get; set; }
}

public class Entry
{
    public int EntryId { get; set; }
    public string Title { get; set; }
    public string EmbedURL { get; set; }
    public string Description { get; set; }
    public Boolean isApproved { get; set; }
    public int UserId { get; set; }

    public virtual ICollection<Submission> Submissions { get; set; }

    public virtual User User { get; set; }
}

public class Submission
{
    public int SubmissionId { get; set; }
    public DateTime Submitted { get; set; }
    public int EntryId { get; set; }
    public int ContestId { get; set; }

    public virtual Entry Entry { get; set; }
    public virtual Contest Contest { get; set; }
}

public class Rating
{
    public int RatingId { get; set; }
    public int Stars { get; set; }
    public int UserId { get; set; }
    public int SubmissionId { get; set; }


    public virtual User User { get; set; }
    public virtual Submission Submission { get; set; }
}

e ha creato un'estensione di DBContext:

public class CPContext : DbContext
{
    public CPContext() : base("name=CPContext")
    {
    }

    public DbSet<Contest> Contests { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<Entry> Entries { get; set; }
    public DbSet<Submission> Submissions { get; set; }
    public DbSet<Rating> Ratings { get; set; }
    public DbSet<Tab> Tabs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

Infine, la stringa di connessione nel mio file web.config:

<add name="CPContext" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet_ContestPlatform;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

Oltre a tutto ciò, sto inizializzando il mio database con i dati di test di seguito:

//Will seed the database with dummy values when accessed for first time
public class ContestPlatformInitializer : DropCreateDatabaseIfModelChanges<CPContext>
{
    protected override void Seed(CPContext context)
    {
        var users = new List<User>
        {
            new User { FirstName = "Daniel", LastName = "Hines", Email = "hinesd@blah.edu" },
            new User { FirstName = "Peter", LastName = "Pan", Email = "panp@blah.edu" },
            new User { FirstName = "Marie", LastName = "VerMurlen", Email = "vermurle@blah.edu" },
            new User { FirstName = "Aaron", LastName = "Brown", Email = "browna5@blah.edu" }
        };
        users.ForEach(s => context.Users.Add(s));
        context.SaveChanges();

        var entries = new List<Entry>
        {
            new Entry { UserId = 1, Title = "Flight Simulation", EmbedURL = "www.blah.com/video1", Description = "This is an awesome app!", isApproved = true },
            new Entry { UserId = 2, Title = "Underwater Explorer", EmbedURL = "www.blah.com/video1", Description = "This is an awesome app!!", isApproved = true },
            new Entry { UserId = 3, Title = "Dress-Up", EmbedURL = "www.blah.com/video1", Description = "This is an awesome app!!!", isApproved = true },
            new Entry { UserId = 4, Title = "Combat Training", EmbedURL = "www.blah.com/video1", Description = "This is an awesome app!!!!", isApproved = true },
            new Entry { UserId = 1, Title = "Fitness Pro", EmbedURL = "www.blah.com/video1", Description = "This is an awesome app!!!!!", isApproved = true }
        };
        entries.ForEach(s => context.Entries.Add(s));
        context.SaveChanges();

        var contests = new List<Contest>
        {
            new Contest { Name = "Game Contest", Description = "This contest is to see who can make the most awesome game!", Start = DateTime.Parse("2012-02-10"), End = DateTime.Parse("2012-04-20"), Published = true },
            new Contest { Name = "App Contest", Description = "This contest is to see who can make the coolest app!", Start = DateTime.Parse("2012-03-10"), End = DateTime.Parse("2012-09-20"), Published = false }
        };
        contests.ForEach(s => context.Contests.Add(s));
        context.SaveChanges();

        var tabs = new List<Tab>
        {
            new Tab { ContestId = 1, Name = "Rules", Content = "The first rule is that there are no rules!", Order = 1 },
            new Tab { ContestId = 2, Name = "Examples", Content = "No examples here yet, check back soon.", Order = 1}
        };
        tabs.ForEach(s => context.Tabs.Add(s));
        context.SaveChanges();

        var submissions = new List<Submission>
        {
            new Submission { ContestId = 1, EntryId = 1, Submitted = DateTime.Parse("2-13-2012") },
            new Submission { ContestId = 1, EntryId = 2, Submitted = DateTime.Parse("2-14-2012") },
            new Submission { ContestId = 1, EntryId = 3, Submitted = DateTime.Parse("2-15-2012") },
            new Submission { ContestId = 1, EntryId = 4, Submitted = DateTime.Parse("2-16-2012") },
        };
        submissions.ForEach(s => context.Submissions.Add(s));
        context.SaveChanges();

        var ratings = new List<Rating>
        {
            new Rating { Stars = 4, UserId = 1, SubmissionId = 1 },
            new Rating { Stars = 5, UserId = 2, SubmissionId = 1 },
            new Rating { Stars = 2, UserId = 3, SubmissionId = 1 },
            new Rating { Stars = 4, UserId = 4, SubmissionId = 1 },

            new Rating { Stars = 1, UserId = 1, SubmissionId = 2 },
            new Rating { Stars = 2, UserId = 2, SubmissionId = 2 },
            new Rating { Stars = 1, UserId = 3, SubmissionId = 2 },
            new Rating { Stars = 3, UserId = 4, SubmissionId = 2 },

            new Rating { Stars = 5, UserId = 1, SubmissionId = 3 },
            new Rating { Stars = 5, UserId = 2, SubmissionId = 3 },
            new Rating { Stars = 4, UserId = 3, SubmissionId = 3 }
        };
        ratings.ForEach(s => context.Ratings.Add(s));
        context.SaveChanges();
    }
}

che viene chiamato nel mio metodo Application_Start () nel mio file Global.asax.

Quindi ora per testare che tutto funziona correttamente, ho creato un controller per il mio modello di concorso, che ha generato una vista corrispondente. Quando compilo la mia applicazione e provo a chiamare il controller del concorso, viene lanciata un'eccezione.

System.Data.EntityCommandexEcutionException non è stato gestito dal codice utente Messaggio = un errore si è verificato durante l'esecuzione della definizione del comando. Vedi l'eccezione interiore per i dettagli. Source = System.Data.Entity StackTrace: at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands (entityCommand EntityCommand, CommandBehavior Behaviour) su System.Data.Objects.Internal.ObjectTeCuctionPlan.Execute [TRESULTYPE Context, ObjectParameTolTection. .Data.Objects.ObjectQuery1.GetResults(Nullable1 exgeoption) su System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() at System.Collections.Generic.List1..ctor (ierumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 Fonte) su ContestPlatform.Controllers.ContestController.Index () in C: Users Danny Documents Visual Studio 2010 Projects ContestPlatform ContestPlatform ContestPlatform Controllers ContestController.cs: riga 21 a Lambda_method (chiusure, Controllerbase, Object []) at system.web.mvc.reflectactionDescriptor.execute (ControlleContext ControlleContext, IDictionary`2 Parameles) su System.Web.Mvc.Async.AsyncControlleractionInvoker. <> C_Displayclass42.b_41 () at system.web.mvc.async.asynccontrolleractioninvoker. <> C_Displayclass37. <> C_Displayclass39.b_33 () su system.web.mvc.async.asynccontrolleractioninvoker. <> C_Displayclass4f.b__49 () INNERException: System.Data.sqlclient.sqlexception Message = Nome oggetto non valido 'dbo.contest'. Source = .NET SQLClient Data Provider ErrorCode = -2146232060 Classe = 16 Linenumber = 1 Number = 208 Procedure = "" Server =. Sqlexpress State = 1 StackTrace: at System.Data.SqlClient.SqlConnection.onerror (SQException Exception, Boolean BreakConnection ( ) at system.data.sqlclient.tdsparser.ThrowExceptionandWarning () su system.data.sqlclient.tdsparser.run (runbehavior runbehavior, sqlCommand cmdhandler, sqldateader. ) at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean ReturnStream, Boolean Async) su System.Data.sqlclient.sqlcommand.runexecuterer (CommandBehavior cmdbehavior, runbehavior runbehavior, booleano returnstream, Metodo stringa, risultato dbasyncresult) su system.data.sqlclient.sqlcommand.runexecuterer (CommandBehavior cmdbeHavior, runbehavior runbehavior, comportamento booleano, metodo di stringa) su system.data.sqlclient.sqlcommand.executer (comandangeavior, motostream di stringa a stringa, metodo di stringa su System.data.sqlclient.sqlcommand .Sqlclient.sqlcommand.executedBDateAder (CommandBehavior Behavior) at System.Data.EntityClient.EntityCommandDefinition.executestoreCommands (EntityCommand EntityCommand, CommandBehavior Behavior) Innerexception:

Notando la riga "Message = Nome oggetto non valido 'DBO.Contest'", volevo ricontrollare che la tabella DBO.Contest fosse effettivamente generata.

Posso entrare in Management Studio Express e vedere il database "Aspnet_ContestPlatform", ma non ha tabelle.

È questo il database che dovrei vedere? Perché non vengono generate tabelle? Inoltre, se le tabelle non ci sono, perché non ricevo un'eccezione proprio quando l'applicazione inizia quando il database dovrebbe essere seminato con i dati di test?

È stato utile?

Soluzione

No, il nome del database dovrebbe essere [ternamespace, se presente] .cpcontext, non "aspnet_contestplatform".

Penso che il motivo per cui non stai ricevendo un'eccezione immediata sia perché è solo quando si colpisce la visione che utilizza il controller che esegue effettivamente i tuoi getResults contro il database che cade. Qualcosa sta impedendo la creazione del database - non sono sicuro di cosa - ma fintanto che non lo selezioni, nulla fallisce all'interno dell'app.

Hai provato a modificare il modello ed eseguire di nuovo l'app? Tengo spesso una proprietà fittizia in una delle mie entità minori che commento alternativamente/spegnere per rigenerare il database. So che non è il modo migliore per fare le cose, ma il modello cambia e quindi il database dovrebbe cadere e ricreare.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top