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?
11 Answers, 1 is accepted
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"
);
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 >>
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
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.
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 >>
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
That was exactly what I needed.
Thank you,
-Thomas
config.HasAssociation(p => p.ContactPersonAttributes).WithOpposite(p => p.ContactPerson).IsManaged().HasConstraint((c, p) => c.Id == p.ContactPersonId).ToColumn(
"ContactPersonId"
);
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);
Object reference not set to an instance of an object
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.
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 >>
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
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.
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 >>
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)
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 >>