1

I am using VisualStudio 2015 and SqlServerExpress2015.

I have 3 Tables in SQLExpress:

SQL Server 3 tables

I startet to create a new WPF Projetct in VisualStudio 2015 I added a new item - Ado.NET Entity Data Model I choose EF Designer from Database

I got this Model.

Visual Studio 2015

I aded a Datasource from Object And added the ProductBase as a Grid. I also add the Validation_Duration_Days and Validation_Amount as Edit Field.

main xaml

Then I dod the follwoing coding.

    public partial class MainWindow : Window
{
    CarWashFlatEntities2 context = new CarWashFlatEntities2();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        System.Windows.Data.CollectionViewSource productBaseViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("productBaseViewSource")));
        // Laden Sie Daten durch Festlegen der CollectionViewSource.Source-Eigenschaft:
        context.ProductBase.Load();
        productBaseViewSource.Source = context.ProductBase.Local;

    }

    private void button_Click(object sender, RoutedEventArgs e)
    {


        context.SaveChanges();
        productBaseDataGrid.Items.Refresh();

    }

    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)

    {

        base.OnClosing(e);

        this.context.Dispose();

    }

}

}

Result is , that I can add BaseProducts. I can also change the duration or validity amount values IF data exist's in the database.

MY question: When I add a row in the list ( productbase) and enter some value in the Vality_Amount textbox.. This will not work. How can I do this ? Adding a new row to ProductBase and also Adding a new row to ProductPrePaid with the same ProductBaseID.

Here are my models: ProductBase.cs

  public partial class ProductBase
{
    public int ProductBaseID { get; set; }
    public string Name { get; set; }

    public virtual ProductFlatRate ProductFlatRate { get; set; }
    public virtual ProductPrePaid ProductPrePaid { get; set; }
}

ProductFlatRate.cs

 public partial class ProductFlatRate
{
    public int ProductBaseID { get; set; }
    public int Validity_Duration_Days { get; set; }

    public virtual ProductBase ProductBase { get; set; }
}

ProductPrePaid.cs

 public partial class ProductPrePaid
{
    public int ProductBaseID { get; set; }
    public int Validity_Amount { get; set; }

    public virtual ProductBase ProductBase { get; set; }
}
Ebc
  • 63
  • 3
  • For starters, don't use partial class for your Models, 2nd just Create another Model class that have the field of all of this 3 classes. You then Enter all of the properties with in them, then just add them to your context. – Aizen Mar 06 '16 at 20:42
  • 1
    You didn't configure your model to understand your hierarchy as you are trying to implement inheritance. See [this](http://stackoverflow.com/questions/22263189/entity-framework-db-first-implement-inheritance/22263190#22263190) answer. – Ashkan Mar 06 '16 at 20:49
  • It is configured properly for CodeFirst approach. But it is partial class, it should be a concrete class. – Aizen Mar 06 '16 at 21:05
  • @Aizen this is not code first. this is database first. As he says `in VisualStudio 2015 I added a new item - Ado.NET Entity Data Model I choose EF Designer from Database`. – Ashkan Mar 06 '16 at 21:12
  • Obviously it is, I am just saying the CodeFirst approach is appropriate for this, cause of the virtual properties. It doesn't do much for Database first. – Aizen Mar 07 '16 at 03:49

0 Answers0