I have a C# project using Entity. I am new to the Entity Framework. I have a singleton class called Project
public sealed class Project
{
public int Id {get; private set}
public static Project Instance { get; private set; }
public string Directory { get; private set; }
public string Name { get; private set;
public void Write()
{
using (var context = new DataContext(new System.Data.SQLite.SQLiteConnection(dbPath), false)
{
context.Projects.Add(this);
context.SaveChanges(); //when only Project is included in my database this works great, but an exception is thrown here whenever I include other classes.
}
}
}
The above class writes to a Projects table beautifully provided I don't try to write any additional tables to the database. However, I get an DbUpdateException whenever I try to add additional classes to the database. For instance, I have an abstract FunctionBase class, a concrete implementation of that class called OrdinatesFunction, an abstract decorator that also implements the FunctionBase while being composed of an OrdinatesFunction, and finally concrete implementations of the abstract Decorator (for instance the OutflowFrequencyDecorator class below). I would like to write the concrete decorators to the database. This shows the basic structure of my codebase:
public abstract class FunctionBase
{
public int Id { get; private set; }
public abstract bool IsValid {get; protected set; }
public void Write()
{
using (var context = new DataContext(new System.Data.SQLite.SQLiteConnection(dbPath), false)
{
context.Functions.Add(this);
context.SaveChanges();
}
}
}
public sealed OrdinatesFunction: FunctionBase
{
public override bool IsValid {get; protected set;}
public Statistics.CurveIncreasing Function {get; private set}
}
public abstract Decorator: FunctionBase
{
public virtual FunctionBase Function { get; set; } //this is a required navigation property I think.
public override bool IsValid { get; protected set; }
}
public sealed OutflowFrequencyDecorator: Decorator
{
public override bool IsValid { get; protected set; }
}
My default assumption with Entity (when my App.Config works and I can write a database file) is that the issue lies in my implementation of the DbContext. This is my DbContext implementation:
public class DataContext: DbContext
{
public DbSet<Projects>
public DbSet<FunctionBase> Functions { get; set; }
public DbSet<OutflowFrequencyDecorator> { get; set; } //I've tried it with and without this and if fails the same way in both instances.
public DataContext(DbConnection connection, bool contextOwnsConnection): base(connection, contextOwnsConnection)
{
}
public override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Project>();
modelBuilder.Entity<FunctionBase>();
modelBuilder.Entity<OutflowFrequencyDecorator>().HasRequired(t=> t.Function);
var sqLiteConnectionInitializer = new SQLite.CodeFirst.SqLiteCreateDatabaseIfNotExists<DataContext>(modelBuilder);
SetInitializer(sqLiteConnectionInitializer);
}
}
At this point I'm pretty lost. I've spent a few hours reading through advice on this site and others. While a lot of folks are encountering the same exception on the same command using Entity, the examples and (I think) reasons seem to be unrelated to my specific issue, or the advice is sparse or contradictory. Any help you could provide would be greatly appreciated... thanks in advance.