Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    DbContext no key defined

    Buonasera a tutti, sono un niubbo con ASP.Net!
    ho questo problema:

    ho creato un nuovo modello con il seguente codice:

    codice:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    
    namespace MvcApplication1.Models
    {
        public class Voti
        {
            [Key]
            public int id { get; set; }
        }
        public class FantacalcioDBContext : DbContext
        {
            public DbSet<FantacalcioDBContext> Voti_Giornate { get; set; } 
        }
    e la seguente connection string:

    codice:
        <add name="FantacalcioDBContext"
             providerName="System.Data.Sqlclient"
          connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Fantacalcio;Integrated Security=True;MultipleActiveResultSets=True"  
        />

    Quando vado a creare un nuovo controller con Modello MVC entity framework mi da questo errore.

    Impossibile recuperare i metadati per xxxxxxx.
    One or more validation errors were detected during model generation:
    - system.data.entity.edmentity type: Entity type FantacalcioDBContext has no key defined. Define key for this entity type.

    Lato DB ho definito tutte le chiavi primarie sulle tabelle, e non riesco a capire a quale key faccia riferimento.

    Grazie ragazzi!

  2. #2
    Utente di HTML.it L'avatar di U235
    Registrato dal
    Mar 2006
    Messaggi
    1,539
    Ciao
    public DbSet<Voti> Voti_Giornate { get; set; }

    Inoltre devi aggiungere un costruttore che accetti la stringa di connessione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.