Fluent Api and Profiler

12 posts, 0 answers
  1. Piotr Jarocki
    Piotr Jarocki avatar
    16 posts
    Member since:
    Feb 2010

    Posted 22 Sep 2011 Link to this post

    Hi,
    I got few questions about Fluent Api
    1. how can i combine Fluent Api with Profiler?
    2. Is there a way to create indexed field with fluent API?
    3. How do i update field name? for example i got field Description and i want to change name to FullDescription, but need to keep all data
    4. How to set isManaged?
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 26 Sep 2011 Link to this post

    Hello Piotr Jarocki,

    1. Yes, you could use the profiler with any OpenAccess domain model, as long as you have enabled the logging functionality in the backend configuration of the context instance. You can find more details about the needed configurations in the following articles:
    How to: Configure OpenAccess Project For Offline Monitoring
    How to: Configure OpenAccess Project For Real-Time Monitoring

    2. Actually there is a way to do this with the fluent mapping api, though there are two thing you need to do. First of all you have to make sure that the properties that you create the index over are mapped to column (use the ToColumn method). Second you have the include the Telerik.OpenAccess.Metadata.Fluent namespace to the list of usings. And then you will be able to use syntax such as 

    customerConfiguration
           .HasIndex(x => new
                   {
                       x.Id,
                       x.PhoneCode
                   })
                   .IsClustered()
                   .IsNotUnique()
                   .WithName("NameOfTheIndex");
    3. For specifying the field name of a property you can just use configuration.HasProperty(...).HasFieldName(string). Though if you want to change the column in the database you have to use the ToColumn method and specify the column name. There is no way though to migrate your data from one column to another with OpenAccees, this is something that you will have  to take care on your own. 

    4. Just use configuration.HasAssociation(...).WithOpposite(...).IsManaged(). Keep in mind though that this will set is managed to true for the property that you have specified in the HasAssociations call. So if it is one to many relationship you should do it on the collection side. 

    I hope this is helpful. 

    All the best,
    Alexander
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

  3. DevCraft banner
  4. Thomas Weidman
    Thomas Weidman avatar
    12 posts
    Member since:
    Apr 2004

    Posted 26 Sep 2011 Link to this post

    Hello-

    For the HasIndex method mentioned, can you provide a better explanation of how that is used? Is that defined in a standard mapping configuration?  I've been trying to trace down this functionality for the Fluent API and have not yet found it.

    Thanks,

    -Thomas
  5. Serge
    Admin
    Serge avatar
    375 posts

    Posted 27 Sep 2011 Link to this post

    Hi Thomas Weidman,

     This is part of the advanced Fluent Mapping API functionalities. The API is actually developed using extension methods so that only the people that have included the Telerik.OpenAccess.Metadata.Fluent.Advanced namespace can see it pop up in intellisense. I see now that I have given you the wrong namespace in the last post, I apologize for that mistake.

    Please try this out and let us know if this works for you. 

    Kind regards,
    Serge
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

  6. Piotr Jarocki
    Piotr Jarocki avatar
    16 posts
    Member since:
    Feb 2010

    Posted 27 Sep 2011 Link to this post

    Hi,
    many thanks for answer in my opinion Fluent API just rocks compared to Design Domain Model (i had a lot of issues, errors etc). Also Profiler is just imba ;) i found some hot spots thanks to that.

    But could u please make and example with isManaged? how to make it on collection side?

    Also it would be very nice if you could add someday something like schemaHandler.DumpDatabase(), so i could dump whole database before update schema, just to be sure i can rollback if something goes wrong, or maybe there is such option?

    Anyway new orm with fluent api is best thing you could create ;) i stopped to use your orm 2 years ago, cos designer just didnt work with databases that are not mssql ;) but now im back and im happy
  7. Thomas Weidman
    Thomas Weidman avatar
    12 posts
    Member since:
    Apr 2004

    Posted 27 Sep 2011 Link to this post

    Serge-

    That was exactly what I needed.

    Thank you,

    -Thomas
  8. Piotr Jarocki
    Piotr Jarocki avatar
    16 posts
    Member since:
    Feb 2010

    Posted 27 Sep 2011 Link to this post

    ive got
    config.HasAssociation(p => p.ContactPersonAttributes).WithOpposite(p => p.ContactPerson).IsManaged().HasConstraint((c, p) => c.Id == p.ContactPersonId).ToColumn("ContactPersonId");
    and 
    config.HasAssociation(p => p.ContactPerson).WithOpposite(p => p.ContactPersonAttributes).IsManaged().HasConstraint((c, p) => c.ContactPersonId == p.Id).ToColumn("ContactPersonId");

    but still when i try to add
    contactPerson.ContactPersonAttributes.Add(contactPersonAttributes);
    ive got error 
     Object reference not set to an instance of an object
  9. Serge
    Admin
    Serge avatar
    375 posts

    Posted 28 Sep 2011 Link to this post

    Hello Piotr Jarocki,

     It looks like the collection property ContactPersonAttributes is not instantiated and this is why you get a null reference exception. What is IsManaged actually does is make sure that the objects in this collection are persisted back to the database, it does not initialize the property for you. 

    Please see whether this solutions works for you and if not please give us a bit more information on the problem, such as a stack trace, and the whole mapping for the classes, as well as the actual classes. 

    I am looking forward to resolving this issue.

    All the best,
    Serge
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

  10. Piotr Jarocki
    Piotr Jarocki avatar
    16 posts
    Member since:
    Feb 2010

    Posted 28 Sep 2011 Link to this post

    i can add objects now, but got another problem :( i cant change mine query to avoid n+1
    here is a query that works but got n+1
    var query = (from c in session.db.TimesheetAttributes
                 where ((c.Active == true)
                        && (c.Timesheet.Active == true)
                        && (c.ValidFrom <= validDate)
                        && (c.ValidTo > validDate)
                        && (c.Timesheet.ContactPersonId == session.ContactPersonAttributes.ContactPersonId))
                 select new
                 {
                     Id = c.TimesheetId,
                     c.Date,
                     WeekDay = c.Date.ToString("dddd", new CultureInfo("pl-PL")),
                     Description = String.Format("{0:dd/MM/yyyy}", c.Date),
                     Hours = String.Format("{0:HH:mm}", (new DateTime((c.TimesheetRows.Aggregate(TimeSpan.Zero, (subtotal, t) => subtotal + (t.DateTo - t.DateFrom)).Ticks)))),
                     c.SickNote,
                     c.Vacation,
                     c.OccasionVacation
                 }).OrderByDescending(c => c.Date).ToList();

    problem is with HOURS because its aggregate, i tried also this query
    var query = (from ta in session.db.TimesheetAttributes
                 join tr in session.db.TimesheetRows on ta.Id equals tr.TimesheetAttributesId into tempTR
                 from ttr in tempTR.DefaultIfEmpty()
                 group ttr by new { ta.TimesheetId, ta.Date, ta.SickNote, ta.Vacation, ta.OccasionVacation } into g
                 select new
                 {
                     Id = g.Key.TimesheetId,
                     Date = g.Key.Date,
                     WeekDay = g.Key.Date.ToString("dddd", new CultureInfo("pl-PL")),
                     Description = String.Format("{0:dd/MM/yyyy}", g.Key.Date),
                     SickNote = g.Key.SickNote,
                     Vacation = g.Key.Vacation,
                     OccasionVacation = g.Key.OccasionVacation,
                     Hours = String.Format("{0:HH:mm}", (new DateTime(g.Aggregate(TimeSpan.Zero, (subtotal, t) => subtotal + (t.DateTo - t.DateFrom)).Ticks)))
                 }).OrderBy(c=>c.Date).ToList();

    and
    var query = (from c in session.db.TimesheetAttributes
                 from cr in c.TimesheetRows
                 where ((c.Active == true)
                        && (c.Timesheet.Active == true)
                        && (c.ValidFrom <= validDate)
                        && (c.ValidTo > validDate)
                        && (c.Timesheet.ContactPersonId == session.ContactPersonAttributes.ContactPersonId))
                 select new
                 {
                     Id = c.TimesheetId,
                     c.Date,
                     WeekDay = c.Date.ToString("dddd", new CultureInfo("pl-PL")),
                     Description = String.Format("{0:dd/MM/yyyy}", c.Date),
                     Hours = String.Format("{0:HH:mm}", (new DateTime((cr.Aggregate(TimeSpan.Zero, (subtotal, t) => subtotal + (t.DateTo - t.DateFrom)).Ticks)))),
                     c.SickNote,
                     c.Vacation,
                     c.OccasionVacation
                  }).OrderBy(c => c.Date).ToList();

    but in both cases i got null object reference... :/ i cant make it to work and with n+1 its getting to slow for more objects, also i need to repair it to change also other queries in mine project
  11. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 30 Sep 2011 Link to this post

    Hi Piotr Jarocki,

     the reason why this happens is that we do not support arbitrary Aggregate methods as server side functions. The best option you have at the moment is to use a fetch plan that avoids the N+1 problem and additionally perform an intermediate .ToList() so that fully populated TimesheetAttribute instances (including the collection of Rows) are available for the in-memory processing.



    var queryTmp = (from c in session.db.TimesheetAttributes 
     where ((c.Active == true) 
     && (c.Timesheet.Active == true) 
     && (c.ValidFrom <= validDate) 
     && (c.ValidTo > validDate) 
     && (c.Timesheet.ContactPersonId == session.ContactPersonAttributes.ContactPersonId)
     orderby c.Date descending
     select c) .ToList();

    var query = (from c in queryTmp
     select new 
     { 
     Id = c.TimesheetId, 
     c.Date, 
     WeekDay = c.Date.ToString("dddd", new CultureInfo("pl-PL")), 
     Description = String.Format("{0:dd/MM/yyyy}", c.Date), 
     Hours = String.Format("{0:HH:mm}", (new DateTime((c.TimesheetRows.Aggregate(TimeSpan.Zero, (subtotal, t) => subtotal + (t.DateTo - t.DateFrom)).Ticks)))), 
     c.SickNote, 
     c.Vacation, 
     c.OccasionVacation 
     }).ToList(); 

    I will take you issue also as a request for a LINQ way to perform date/time duration aggregates on the server and file a respective enhancement request.

    All the best,
    Thomas
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

  12. Piotr Jarocki
    Piotr Jarocki avatar
    16 posts
    Member since:
    Feb 2010

    Posted 04 Oct 2011 Link to this post

    hi,
    go another problem ;) ive created project on 1 pc, and everything is working fine, but when i copied mine source code to another pc, it compiled fine but in CreateUpdateDDLScript i got exception:

    No metadata has been registered for class Gambu.Model.Contact. (This usually indicates, that either this class is not declared persistent or it is declared persistent but not enhanced)
  13. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 06 Oct 2011 Link to this post

    Hello Piotr Jarocki,

     The message you are seeing usually indicates that the assembly containing your model has not been enhanced successfully. Is it possible that the our enhancer is not present on the machine you are trying to run the sample on? If the enhancer is indeed present please check the OpenAcess.taget tag in your project file and verify that it points to the correct location for the other PC as well.

    Regards,
    Petar
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

Back to Top
DevCraft banner