Long time listener, first time caller.
I'm probably taking the wrong approach to this, so any help would be fantastic.
I'm using C#, MVC 4, Code First, Entity Framework 4.3, & Razor.
I am trying to split out EF data classes & some methods into a separate class project so that they can generically be references by multiple other projects. I've created this simplified example to highlight the issue as much as I can.
So in a class project I have:
public MyGeneric(string name)
{
ID = Guid.NewGuid();
Name = name;
}
public Guid ID { get; set; }
public string Name { get; set; }
public void AddThis(MyGeneric obj)
{
using (var db = Activator.CreateInstance<myDbContext>())
{
var myOthers = from mo in db.MyOther
where mo.OtherID == obj.ID
select mo;
myOthers.ForEach(mo => db.MyOther.Add(mo));
db.SaveChanges();
}
}
And then I can reference this in a main project such as:
public class SampleInherit : MyGeneric
{
public SampleInherit(string newName, string secName)
: base(newName)
{
SecName = secName;
}
public string SecName { get; set; }
public void DoSomething(SampleInherit obj)
{
base.AddThis(obj);
// do other stuff
}
}
What I'm trying to do is genericise (is that a word?) a few classes with their methods into a separate side project. This side project will supply base classes that will (in order of desired):
- use EF DbContext to create/access the database,
- allow more properties to be added,
- supply method/procedures that affect the underlying data,
- be renamed more appropriately for the main project.
Now if I just inherit the base classes into classes in the main project, it all works except for the base class reference myDbContext & the entity object references within the LINQ statements.
Can you late-bind the DataContext or have I painted my self into a corner here?
Cheers for all the help you can give.