Help get this topic noticed by sharing it on Twitter, Facebook, or email.

Entity framework with COT, how i maded.

I've ever used Entity Frameworks (EF) in my projects and i love to use it in COT as well.
This implementation uses EF transparently on the server side. Commands like "Insert", "Update" and "Delete" of standard DataAquarium are "trapped" by EF and handled consequently.
DataAquarium Model's remains untouched. I only added the EF. COT will use his own DataAquarium model for all, and don't care about EF. The only thing EF makes, is to override the default behaviour of the DataAquarium.

COT 8.6.2, Touch UI, SPA.
Prerequisite: i well know EF and c#, i'm an experienced developer. Don't do what i've made if you are a novice.

Step 1: i created a Folder under the "Model" folder of your project.For Example "/Model/AutoGen"


Step 2: I generate entities using Database first implementation. I do this by right click on the "AutoGen" folder and selecting "Add new item", then "Ado.NET Entity Data Model". I put a name i want, click OK and then select "Code First From Database"

Step 3: Well, i have EF in my project. I just referenced it using "using" in my business rules and in my SharedBusinessRule.

Step 4: I created a shared business rule on COT. Warning: if you've created own business rules, these business rules must inherits from the shared business rule, otherwise you rules will not work anymore.

Step 5: I added this to my SharedBusinessRule.cs file:


The content of both BeforeSqlAction and AfterSqlAction should be:

protected override void BeforeSqlAction(ActionArgs args, ActionResult result)
{

if (args.Controller == "Insert here your controller's name")
{
if (args.CommandName == "Insert")
{
result.Canceled = true; // This overrides the default behaviour of the DataAquarium. You'll have to handle the "insert" with EF.
}
else if (args.CommandName == "Update")
{
result.Canceled = true; // This overrides the default behaviour of the DataAquarium. You'll have to handle the "Update" with EF.
}
else if (args.CommandName == "Delete")
{
result.Canceled = true; // This overrides the default behaviour of the DataAquarium. You'll have to handle the "Delete" with EF.

// Example on how to retrieve the current ID of the record you'll delete
int ID = int.Parse(args.Values.Where(a => a.Name.ToUpper() == "your entity unique id name in UPPERCASE, for example CUSTOMERID").FirstOrDefault().NewValue.ToString());

}

}
}

That's all.
Two things to remember:

1) From now, all commands, actions or code that calls "Insert", "Update" and "Delete", will be trapped and the DataAquarium will not perform these actions. I must perform the actions by myself, using the ID of the field passed in the "args" parameter.
2) Every time i change my Database (for example to add a new field), i must update COT Model as usual and refresh my "AutoGen" folder as well, by deleting all content and perform the step number 2.

Please read this post at your own risk, if you know what you're doing. I assume no responsibility for damage you cause yourself or to others if you do this.
2 people like
this idea
+1
Reply