T4 to generate DTO' and Nullable Datatypes - t4

I'm trying to generate DTO's with T4. I found a great blog post that does exactly what i'm looking for but it explodes on Nullable data types.
http://weblogs.asp.net/cibrax/archive/2009/03/11/code-generation-with-t4-an-entities-to-dto-example.aspx?CommentPosted=true#commentmessage
This generates code with Nullable types like below
[DataMember(Name="terminationCFDate")]
public System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] TerminationCFDate
{
get; set;
}
I am looking for something like below
[DataMember(Name="terminationCFDate")]
public Nullable<DateTime> TerminationCFDate
{
get; set;
}
I am very new to T4. Any suggestions?

Modified the template. It was using reflections.

Related

How to map array of dates to elastic search?

I have an array of dates and I want to create the mapping by Nest.
But my current mapping searches do not work. Query sample is:
publicationDates: '1995-01-11'
My mapping:
[ElasticsearchType(Name = "doc-index", IdProperty = "Id")]
public class DocumentIndex
{
[Keyword(Index = false)]
public string Id { get; set; }
[Text]
public string Title { get; set; }
[Date(Format = "yyyy-MM-dd")]
public IEnumerable<DateTime> PublicationDates { get; set; }
}
The Format parameter tells Elasticsearch how to parse the incoming string into a date on the server side, but it doesn't affect the way that DateTime are serialized by the client, which serializes DateTime to ISO8601 format using Json.NET internal DateTime serialization handling.
If you'd like to serialize DateTime for your POCO differently, the easiest way is to implement a JsonConverter and attribute the PublicationDates property to use this converter. This is all that is needed for NEST 5.x and previous.
In NEST 6.x, the Json.NET dependency is internalized, so if you're using NEST 6.x, you need to also use the NEST.JsonNetSerializer package and hook up the serializer so that serialization of your types is delegated to Json.NET.

SQLite-net-pcl - Always returning ID as 0 (Xamarin)

I recently moved across from SQLite.NET to SQLite-net-pcl due to the Android 7.0 SQlite issue.
In the process I have updated all my libraries and all is working well with regards to insert/drop etc.
The only issue I have is that every time I retrieve an item from the DB it always has an ID of 0. This is causing problems with updating items. Has anyone had this issue before?
[SQLite.PrimaryKey, SQLite.AutoIncrement]
public int ID { get; set; }
public string objectId { get; set; }
public DateTime createdAt { get; set; }
public DateTime updatedAt { get; set; }
public string Title { get; set; }
Thanks in advance.
Please try using this, this worked for me
using SQLite.Net.Attributes;
[PrimaryKey, AutoIncrement]
public int? Id { get; set; }
Had almost the same situation.
with all rows returning id of 0
class ToDo : Java.Lang.Object
{
[PrimaryKey, AutoIncrement]
public int ID { get; }
except I simply had forgotten to type set; in the property.
one thing that might help someone with this sorta problem is using the
GetMapping() method to get some info on the mapping used when CreateTable() is used to create the table.
as example:
Toast.MakeText(this, db.GetMapping<ToDo>().TableName.ToString(), ToastLength.Long).Show();
Toast.MakeText(this, db.GetMapping<ToDo>().HasAutoIncPK.ToString(), ToastLength.Long).Show();
using this I found out that AutoIncrement (HasAutoIncPK = false) wasn't being set on my table.
See if you created the table with the methods CreateComand(query) and ExecuteNonQuery(). If this is the case, create your table with the CreateTable<Type>() method. The primary key and autoincrement attributes are initialized at the time of creating the table through said method
I have been struggling with the same issue here.
I was manually creating the tables to guarantee a smoother update process moving forwards rather than using the CreateTable methods available.
The fix that I eventually stumbled upon was that I was using the wrong data type for my PRIMARY KEY column.
Wrong definition
[RecordIndexId] int PRIMARY KEY NOT NULL
Correct definition
[RecordIndexId] integer PRIMARY KEY NOT NULL
To add a little context there is a big different between the int and integer data types. Explained in this SO answer
My problem was that I had an internal set for my ID property setter. This had to be public to work correctly.
I'd add this as a comment but alas... I don't have enough rep.

comment from database in EF

Is it possible to import oracle database comment on columns of table into generated EF classes as [DisplayName("column comment")] annotation for every generated property?
for example you have this Table
employee
column-name type comment
ID integer Personnel Code
NAME varchar2(30) Employee Name
ADDData date Start Date
and when create EF class from this table I like have this generated class
public class Employee
{
[DisplayName("Personnel Code")]
public int Id { set; get; }
[DisplayName("Employee Name")]
public string Name { set; get; }
[DisplayName("Start Date")]
public DateTime AddDate { set; get; }
}
Yes, you can generate your Entity Contexts & POCOs with T4 text templates.
From Wikipedia
Microsoft's Text Template Transformation Toolkit (usually referred to as "T4") is a template based text generation framework included with Visual Studio. T4 source files are usually denoted by the file extension ".tt".
T4 is used by developers as part of an application or tool framework to automate the creation of text files with a variety of parameters. These text files can ultimately be any text format, such as code (for example C#), XML, HTML or XAML.
There's a video example by Julie Lerman here.
Here is a great utility for creating multiple files from a single T4 template.

Entity Framework 4 and MVC 3 - Inheritence and Loading Derived Entities

I've got some common properties (CreatedBy, CreatedOn, UpdatedBy, UpdatedOn) for nearly all of my entities. I decided to have a BaseEntity with these properties and then have all of my other entities inherit from this one.
For example:
BaseEntity --> Question
BaseEntity --> Answer
Now how do I load questions from my model container?
There is no db.Questions or db.Answers any more. Just db.BaseEntities.
Specifically I want to load all questions by their subject - so normally I would say db.Questions.Where(q => q.Subject.Id.Equals(subjectId)).
How do I do this now?
Many thanks,
Pete
First, there are multiple types of inheritance mapping strategies that you can use to configure your database
Table per hierarchy (TPH) - this is used by default, so currently you get only one table in database
Table per type (TPT)
Table per concrete type (TPC)
You can also add sets of concrete types to your DbContext
public class MyDbContext : DbContext
{
public DbSet<Question> Questions { get; set; }
public DbSet<Answer> Answers { get; set; }
}
And the most important - I would prefere composition over inheritance in your place. This feels more natural
public class HistoryRecord
{
public User CreatedBy { get; set; }
public User UpdatedBy { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime UpdatedOn { get; set; }
}
and use it as complex type inside Question, Answer, etc
public class Question
{
public HistoryRecord HistoryRecord { get; set; }
}
Do not use a base class for this.
You'll end up with one entity set and have to use typeof(X).
Worst of all, this may break future inheritance of other entities, especially if some are table per type, some are table per hierarchy and some are mixtures of the two.
Instead, use partial classes to define a common interface with the aspect properties (CreatedBy, CreatedOn, UpdatedBy, UpdatedOn).
Edit: The base entity will also require a table with the PKs of all your entities in it.
This will cause the table to be unnecessarily large and may result in performance hits during queries and inserts.

Populate DTO from several queries

I have a DTO with 40+ properties. But in order to populate all properties I need to execute 4 separate queries. My first query is in charged of getting basic information. For each row returned I run 3 more queries based on the id's given from the main query (N+1 problem). I can set use eager loading but then I'm loading thousands of objects that I don't need.
Should I split my DTO and create a separate DTO for each query I run then link then tie them all together into a central DTO by id?
I was envisioning a final DTO like this.
public class FooDto
{
public string Foo { get; set; }
public string Bar { get; set; }
public FirstDto FirstQueryResults { get; set; }
public SecondDto SecondQueryResults { get; set; }
public ThirdDto ThirdQueryResults { get; set; }
}
Is there a better way of solving this? I'm using Oracle and NHibernate doesn't support multi criterias. Note that I am joining most of my data. The problem comes when I need to query data with a complete new set of criteria.
How about creating a VIEW that joins the data to give the 40 properties all in one go, and basing your DTO on that - whatever a DTO may be ;-)

Resources