Y
Y
yoursweetboybrawlstars2022-03-16 14:15:19
Entity Framework
yoursweetboybrawlstars, 2022-03-16 14:15:19

Error in wpf project when working with sqlite db, how to solve?

The error itself:

System.Data.Entity.ModelConfiguration.ModelValidationException: "One or more validation errors were detected during model generation:
CalculatorMaining.Classes.video_card: : EntityType 'video_card' has no key defined. Define the key for this EntityType.
video_cards: EntityType: EntitySet 'video_cards' is based on type 'video_card' that has no keys defined.

Model Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace CalculatorMaining.Classes
{
    [Table("video_cards")]
    class video_card 
    {
        [Key]
        private string model;
        private double consumption, income;

        public double Consumption
        {
            get { return consumption; }
            set{ consumption = value;}
        }

        public double Income
        {
            get { return income; }
            set { income = value; }
        }

        
        public video_card() { }
        public video_card(double consumption, double income, string model)
        {
            this.consumption = consumption;
            this.income = income;
            this.model = model;
        }
    }
}

Context Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace CalculatorMaining.Classes
{
    class ApplicationContext : DbContext
    {
        public DbSet<video_card> video_cards { get; set; }

        public ApplicationContext() : base("DefaultConnection") { }
    }
}

main code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CalculatorMaining.Classes;

namespace CalculatorMaining
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Classes.ApplicationContext db;
        public MainWindow()
        {
            InitializeComponent();

            db = new Classes.ApplicationContext();
        }

        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            video_card video_card = new video_card();
            db.video_cards.Add(video_card);
            db.SaveChanges();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question