0

Abstract:

I have am trying my hand at ef 6 and mvc 5 for a company project and cannot seem to get my fluent API to work for one of my mappings, it throws various errors depending on what I try, nothing has worked since I added two fields to one of my data models.

OverView:

I have a table that holds 5 pieces of data about ERP Systems

  1. Int Id - key
  2. String Name
  3. bool SystemDatabase
  4. ref to SQL Statement - needs to be required
  5. ref to SQL Statement - needs to be required

I have another that holds all of the programs SQL Statements

  1. Int Id - key

  2. String Description

  3. String Query

  4. Int SQLType

  5. Int? ModuleId

  6. Int? Order

  7. bool Multistep

  8. ref ERPSystem - foreign key

    I am populating most the data on database creation, I was able to have all of this running before I added the need for the two SQL statement references in the ERP table, now I either I recieve a foreign key constraint error on the SQLStatement table regarding the ERPSystemID, a multiplicity error on the ERPSystem table, or my ERPSystem reference is null during the seeding of the data, depending on what method I am flailing around in implementation to get this working.

Code:

public enum SQLType { Create = 0, Insert = 1, Validation = 2, Direct = 3, Determination = 4  }

[DataContract(IsReference = true)]
public class SQLStatement
{
    public int Id { get; set; }
    public string Description { get; set; }
    public string Query { get; set; }
    public SQLType SQLType { get; set; }

    public int? ModuleId { get; set; }

    public int? Order { get; set; }

    public bool MultiStep { get; set; }
    public virtual ERPSystem ERPSystem { get; set; }
    //public virtual ICollection<Parameter> Parameters { get; set; }
}

[DataContract()]
public partial class ERPSystem
{
    public ERPSystem(){}

    [DataMember]
    public int ERPSystemId { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public bool SystemDatabase { get; set; }

    [DataMember]
    public virtual SQLStatement AppDBDetermination { get; set; }

    [DataMember]
    public virtual SQLStatement CompanyDetermination { get; set; }
}

class SQLStatementMap : EntityTypeConfiguration<SQLStatement>
{
    public SQLStatementMap()
    {
        HasKey(x => x.Id);
        Property(x => x.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        HasRequired(x => x.ERPSystem);
    }
}

class ERPSystemMap : EntityTypeConfiguration<ERPSystem>
{
    public ERPSystemMap()
    {
        Property(x => x.ERPSystemId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        HasKey(x => x.ERPSystemId);
        //Figuring these out
        HasRequired(x => x.AppDBDetermination);
       
    }
}

public class DALContextDBInitializer : DropCreateDatabaseAlways<DALContext>
{
    protected override void Seed(DALContext context)
    {
        List<SQLStatement> SQLStatements = new List<SQLStatement>();
        //ERPSystems
        ERPSystem Solomon = new ERPSystem() { Name = "Solomon", SystemDatabase = false };
        context.ERPSystems.Add(Solomon);
        //End ERPSystems
        /*
            unrelated stuff not included in pastebin
        */
        SQLStatements.Add(new SQLStatement() { Description = "Retrieve Solomon Application DB", Query = @"SELECT DatabaseName FROM Domain Where recordtype = 1", ERPSystem = Solomon, SQLType = SQLType.Determination, ModuleId = null, Order = 0, MultiStep = false });
        SQLStatements.Add(new SQLStatement() { Description = "Retrieve Solomon Active Companies", Query = @"SELECT DatabaseName, CpnyID FROM Company WHERE Active = '1'", ERPSystem = Solomon, SQLType = SQLType.Determination, ModuleId = null, Order = 0, MultiStep = false });
        context.SQLStatements.AddRange(SQLStatements);

        Solomon.AppDBDetermination = SQLStatements[0];
        Solomon.CompanyDetermination = SQLStatements[1];
        base.Seed(context);
    }
}

Conclusion:

Any and all advice would regarding this problem would be appreciated, I am already working on improvements for Best practices not related this particular problem (i.e. WCF and serialization)

Community
  • 1
  • 1
Joshua Van Hoesen
  • 1,684
  • 15
  • 30

0 Answers0