Question

I'm using razorengine to generate some code I think that's straight forward but I'm getting an exception where it previously worked just fine.

private FileItem Parse(string template, Model dataitem)
{
    _content = Razor.Parse(template, dataitem );
    // return current instance for method chaining ...
    return this;
}

dataitem is just an object that contains an ef entity:

{System.Data.Entity.DynamicProxies.Table_600FE9F0407FBF4EABE3C512A1938F3D58ACD5ABD32691FF564D071856338EBB}

Template contains a meaningless string: "(Some text)"

Yet I'm getting this exception:

Unable to compile template. The type or namespace name 'DynamicProxies' does not exist in the namespace 'System.Data.Entity' (are you missing an assembly reference?).

I've installed ef with nuget so I assume that all references are there. I've tried both with ef5 and ef6 same result. I'm trying to do this:

string template = "Hello @Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });
Was it helpful?

Solution

The simple answer is to turn off lazy loading. It will stop ef from wrapping the table data in proxy objects which cause the problem. However if you have lots of navigation properties they will stop loading automagically too.

The second answer to force the query to preform by using ToList() after your linq statement which removes the proxy objects too and then pass it into razor as data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top