entity framework "Code first from Database" ObservableCollection and .t4 file - entity-framework

My ultimate aim is to have an EF model working alongside a sqlite local db. The dev is going to involve a lot of modification of the db and thus a lot of updating of the model. The gui will use a lot of Master-Detail views to edit the data.
I've figured that in order to get master-detail views working, you have to replace ICollection and HashSet with ObservableCollection. I achieve this via a custom ObservableListSource class.
I've given up on trying to achieve this via "database first." There's to many limitations/omissions on the "update model from database" option. For example, it doesn't update any changed fields or relationships, meaning you have to delete and re-impot the whole model again. You then have to change the .tt files to change Hashset and ICollection to the ObservableListSource. Overall the method just feels too shonky and hit and miss.
Thus, looked into the "Code first from Database" option. On initial tests appears a lot more robust and reliable than database first. However, it doesn't generate and .tt files, which would allow you to change all of the ICollection entries in the generated classes.
I did find this:
Changing the generated classes from "Code First From Database" EF6
And successfully downloaded the .t4 file. I managed to get HashSet replaced by ObservableListSource by amending some code. Extract of the fuill entity.t4 file:
Public Sub New()
<#
foreach (var collectionProperty in collectionProperties)
{
#>
<#= code.Property(collectionProperty) #> = New ObservableListSource(Of <#= code.Type(collectionProperty.ToEndMember.GetEntityType()) #>)()
<#
}
#>
End Sub
Now this works..when the classes/entities are generated, the replacement occurs correctly. However, I cannot get the same to work for ICollection. An example of how this in generated in the entity class:
<Table("dbControl")>
Partial Public Class dbControl
Public Sub New()
dbController_Controls = New ObservableListSource(Of dbController_Controls)()
End Sub
Public Property ID As Long
<StringLength(2147483647)>
Public Property Name As String
Public Property ControlTypeID As Long?
Public Property ControlAssociationID As Long?
Public Overridable Property dbControlAssociation As dbControlAssociation
Public Overridable Property dbControlType As dbControlType
Public Overridable Property dbController_Controls As ICollection(Of dbController_Controls)
End Class
The relevant part of the EntityType.t4 appears to be:
foreach (var navigationProperty in entityType.NavigationProperties)
{
if (!first)
{
WriteLine(string.Empty);
}
else
{
first = false;
}
#>
Public Overridable Property <#= code.Property(navigationProperty) #> As <#= code.Type(navigationProperty) #>
<#
}
#>
End Class
However, there is no specific reference in the file to ICollection anywhere. Therefore a straight replace doesn't seem to be possible, unlike HashSet
Does anyone have any ideas?
I'm coming close to ditching Entity Framework all together tbh, as it's proving to be a little temperamental/infuriating! Any help appreciated

Related

Breeze: How to make use of entity constructor code on server?

The question could also look like "Why is my initialization code of object in server not working?".
For example,
public class Order
{
public int Id { get; set; }
public int Quantity { get; set; }
public Order()
{
Quantity = 10;
}
}
From debugger, I can see the contructor is called and Quantity is set, however, it is not taking effect. I have to set Quantity on client side after the entity is created to make it work.
Is there a way to make the initialization on server work? By the way, my project is in Angular/Breeze/EF.
UPDATE: As I dig a little further, I believe, this is the general "issue" with Breeze that the server side change must be added to so-called entityInfo.OriginalValueMap, otherwise, its change is not saved. If true, how can work around this limitation because I have a lot default values I'd like to set on server?
This is how I create my entity:
var manager = new breeze.EntityManager("breeze/breeze");
manager.enableSaveQueuing(true);
function _createEntity(entityName) {
return manager.createEntity(entityName);
}
Setting any initialization code on the server in the model constructor won't work simply because the JavaScript client doesn't know anything about the C# constructor code on the server.
The DefaultValueAttribute is only honored when you're constructing a Model-First metadata. It is unfortunately ignored by EF when constructing a Code-First model metadata.
I suggest that you see Breeze - Create Entity on Server side for how another user solves a similar situation by creating a "create Endpoint" on the server that basically returns a new entity with default values set.
You don't have to create a constructor to set default values.
Just add the default value data annotation to any property you wish to set its default value:
[DefaultValue(10)]
public int Quantity { get; set; }
Also, consider not to initialize the Quantity when creating an entity at the client side.

How to avoid lazy loading in play framework

In play framework, I use following code to fetch values from a table called "Report" which has other relationship tables like "Project","Build" etc.
List<Report> rpts = Report.find.where()
.eq("publish","1")
.eq("functionality_id", Integer.toString(fun.id))
.eq("project_id", currentProject.id)
.eq("prod_build", prod_build)
.eq("loadType_id", loadType_id)
.in("build_id", buildId)
.orderBy("id desc")
.findList();
I get list of values from "Report" table, but all related table values are not populated. They are populated with null.
#Entity
#Table(name="report")
public class Report {
#Id
public int id;
#Constraints.Required
#ManyToOne
#JoinColumn(name="build_id")
public Build build;
#Constraints.Required
#ManyToOne
#JoinColumn(name="project_id")
public Project project;
.
.
}
It was loaded with those values when I tested couple of days ago, but not working today. When I do rpts.get(i).build.release , it gives nullPointerException because build is null here. This code has not been changed in recent days. Wondering why this is happening. Can someone suggest me whether there is any other setting file (like application.conf) that does this lazy loading. Please help.
I've resolved it.
The problem is that I created an Ebean transaction using following code that caused the trouble.
Ebean.beginTransaction();
try{
group.role = "A";
Ebean.update(group);
Ebean.commitTransaction();
} finally {
Ebean.endTransaction();
}
I never suspected that this could would have caused the problem as I put this code in another class file that is not related to this page. Then I changed to following code and everything worked as expected.
#Transactional
public void saveGroup(Group group){
group.role = "A";
Ebean.save(group);
.
.
}
Following documentation in play framework site helped me to identify the culprit. :)
Enhancement of direct Ebean field access (enabling lazy loading) is
only applied to Java classes, not to Scala. Thus, direct field access
from Scala source files (including standard Play 2 templates) does not
invoke lazy loading, often resulting in empty (unpopulated) entity
fields. To ensure the fields get populated, either (a) manually create
getter/setters and call them instead, or (b) ensure the entity is
fully populated before accessing the fields.

Breeze Expand not working on WebAPI with EF

I have published a WebAPI service which returns a list of items. I am implementing Breeze and have managed to get it basically working with filtering/sorting. However, the Expand is not working.
http://www.ftter.com/desktopmodules/framework/api/dare/dares?$expand=ToUser
You can see the ToUserId ForeignKey in the response above, but the ToUser properties are NULL (the user definitely exists)
You can see the ToUser EF navigation property in the metadata.
When I use .Include on the server side I can populate it with EF, but I don't want to do this.
I checked the Breeze Tutorial 2 here on Expand: http://learn.breezejs.com/
Here is it without expand: http://learn.breezejs.com/api/northwind/Products
and here it is with Expand (And you can see the additional Category info): http://learn.breezejs.com/api/northwind/Products?$expand=Category
This is what I am trying to do but mine does not fill it...
UPDATE:
I downloaded the Breeze 1.3.6 Samples and loaded the DocCode solution in VS2011.
I ran it and saw that the client-side expand works;
e.g.
http://localhost:47595/breeze/Northwind/Orders?$top=1 (no expand)
http://localhost:47595/breeze/Northwind/Orders?$top=1&$expand=Customer (expands customer correctly).
I checked the WebAPI controller code and it looks the same, except they use EF Code First instead of Model First. The Foreign key is decorated with a property:
Breeze Sample that Works
[ForeignKey("CustomerID")]
[InverseProperty("Orders")]
public Customer Customer {get; set;}
It just doesn't make sense... it is something to do with my WebAPI controller or EntityFramework relationship...
UPDATE 2
I downloaded the most basic ToDo Knockout Breeze sample and added this line to the ToDoItem class: public User ToUser { get; set; }
I am then able to Expand the WebAPI call with http://localhost:63030/breeze/todos/Todos?$expand=ToUser
So I have come to the conclusion that it is something to do with the fact that I am using EntityFramework DB First and not Code First. It definitely does seem possible to do in the current version of the WebAPI with Breeze and EF.
UPDATE 3
I have narrowed it down to my database, EF Database First and Code First differences, but still not identified the issue. I have changed from a Model to a Code First approach with the exact same result (ie. no expand).
For example: if you look at this Expand on the Breeze site that works, http://learn.breezejs.com/api/northwind/Products?%24expand=Category, try change the last param to an invalid field and it throws an error, e.g. :
http://learn.breezejs.com/api/northwind/Products?%24expand=Category1
However, in my code, it always ignores this param and returns ALL the records, and never throws an exception if the Expand param is incorrect:
http://www.ftter.com/desktopmodules/framework/api/dare/dares?$expand=To4657657User
Hence I am stumped.. I have no idea why this is not working.
My Code
[HttpGet]
[Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
public HttpResponseMessage Dares()
{
var response = Request.CreateResponse(HttpStatusCode.OK, (IQueryable<Dare>)contextProvider.Context.Dares);
return ControllerUtilities.GetResponseWithCorsHeader(response);
}
and here is the generated class from my EF model (using Database First)
public partial class Dare
{
public int DareId { get; set; }
public int ToUserId { get; set; }
public virtual User ToUser { get; set; }
}
Your URL seems to be missing the $ for the expand query option...should be $expand.
I think I have found the problem - the IQueryable with the HttpResponseMessage return type does not behave the same as a pure IQueryable return type. expand seems to work when I do not wrap it.
I have raised a new question here:
How to use Breeze IQueryable with CORS?

Can I get decode an EntityFramework Model from a specified migration?

Apparently IMigrationMetadata.Target encodes the state of the EF model.
Can I use this to reconstruct the model for a particular migration?
Yes, it is possible. I was myself curious what exactly those magic resource strings were storing. By digging into the Entity Framework source (see the DbMigrator.GetLastModel() method), I found out that the IMigrationMetadata.Target just stores a base-64 string containing gzipped XML data. To test this, I created a new console application containing a simple code-first model defined as follows:
public class ContactContext : DbContext
{
public virtual IDbSet<Contact> Contacts { get; set; }
}
public class Contact
{
public int Id {get; set;}
public string FirstName { get; set; }
public string LastName { get; set; }
}
Then I created a migration using the NuGet Package Manager Console:
PM> Enable-Migrations
PM> Add-Migration MyMigration
Next I added the following code to my application's Main() method to decode the value in that string and dump it to the console:
var migration = new MyMigration();
var metadata = (IMigrationMetadata)migration;
var compressedBytes = Convert.FromBase64String(metadata.Target);
var memoryStream = new MemoryStream(compressedBytes);
var gzip = new GZipStream(memoryStream, CompressionMode.Decompress);
var reader = new StreamReader(gzip);
Console.WriteLine(reader.ReadToEnd());
This outputs an EDMX file representing the Entity Data Model associated with my DbContext that created the migration. If I write this output to a file with the .edmx extension, I'm able to open it with Visual Studio and view it in the Entity Designer.
Then if for some reason I wanted to regenerate the DbContext and entity classes that produced the model, I would need only do the following:
Add the .edmx file to a Visual Studio project.
Install the EF 5.x DbContext Generator for C# if I don't already have it.
Add the related T4 templates by selecting Add -> New Item from project node context menu.
Modify the newly added .tt files, replacing $edmxInputFile$ with the name of my .edmx file.
Watch as the two templates magically regenerate my code-first types to their respective .cs files.
Hope that answers your question! :-D
I created a small console app to export EDMX from the Model column of the __MigrationHistory table https://github.com/andreydil/EfMigrationModelDecoder
You can choose specific migration using /migration parameter, i.e:
EfMigrationModelDecoder.Cli.exe "<connectionString here>" /migration:Init
I created a PowerShell script to extract the latest migration from a DB to a edmx-file.
https://gist.github.com/otto-gebb/93d021c8fd300646dba0073a77585a94
You can also use SQL...
SELECT CONVERT(xml, DECOMPRESS(Model)) FROM [dbo].[__MigrationHistory] WHERE MigrationId = 'NameOfMigration'

EF 4.1 DbContextGenerator object names - can they be changed?

I am using DB First EF 4.1 and I am adding DbContextGenerator tt template to my model. This is all great, but I end up with classes like this:
public partial class t_city
{
public t_city()
{
this.t_neighborhood = new HashSet<t_neighborhood>();
}
public int city_id { get; set; }
public string city_name { get; set; }
public virtual ICollection<t_neighborhood> t_neighborhood { get; set; }
}
This is super ugly. I modified the template to generate properties in camelcase, but that breaks the mapping onto tables and columns. Is there way to get clean class names and still preserve the mapping?
EDIT
Looks like it's possible by renaming the objects inside the Entity Model file. The only question remains, is it possible to automate the renaming using a function, or does it have to be done manually each time?
Thanks!
You need to do it manually but that's needed only once for each entity / property. These changes are not deleted when you update your model from the database.
The only automation can be implemented as some processing of EDMX file. It is XML with defined schema so you can process that XML in your custom tool or XSLT transformation and automatically change property and entity names in CSDL and MSL.