objectcontext vs datacontext - vb.net

i'm just new in developping silverlight, and i created a linq to sql connection and a service domain class. I want to get data from 2 tables which have a 1 to many relation into a datagridview. To do this i need to state include commands in my metadata and service domain class , but to do this i need to have an objectcontext instead of a datacontext(that i'm currently having ) can someone help me with this matter so i can use the include statement to get querys for my detail-grid
edit:
I've done what u said added the
"<IncludeAttribute()> _"
Public Property SubgroepIndustries As EntitySet(Of SubgroepIndustrie)
but i get this error message:
Error 1 'Include' is not a member of 'System.Data.Linq.Table(Of ILA4.HoofdgroepIndustrie')
edit 2:
when i try to use the include in my domain service class not the metadata so
Return Me.DataContext.HoofdgroepIndustries.Include("SubgroepIndustries")
doesnt work

ObjectContext is a class that is generated inside the generated DomainService class that you made.
Just do a this.ObjectContext in the DomainService class you made and you should have access to the class you are looking for.
I have assumed here that you are using RIA services and your DomainService MetaData class is tagged with [Include] attributes. Otherwise doing this.ObjectContext.SomeEntity.Include("ChildEntity") will not work out.
Edit:
Add <IncludeAttribute()> _ to Public Property SubgroepIndustries As EntitySet(Of SubgroepIndustrie) in your .metadata.vb
As for ObjectContext, looking at your code you don't need ObjectContext I think. Use DataContext instead.
so for example:
Public Function GetHoofdgroepIndustries() As IQueryable(Of HoofdgroepIndustrie)
Return Me.DataContext.HoofdgroepIndustries.Include("SubgroepIndustries")
End Function
is how you will do it.
Edit 2: You need Imports System.ServiceModel.DomainServices.Server for <IncludeAttribute()>

Related

wcf client and sending objects to server

I have a question...
I have a web service where the OperationContract are retrieve and update.
Using a cars example, I have the retrieve providing an object that contains a list of cars and how many cars. I have that configured through a class.
[OperationContract(Name = "**Retrieve**")]
[FaultContract(typeof(FaultInfo))]
[XmlSerializerFormat]
CarInfo Retrieve(CarRequest CarRetrieve);
[OperationContract(Name = "**Update**")]
[FaultContract(typeof(FaultInfo))]
[XmlSerializerFormat]
CarUpdateInfo Update(CarUPDRequest CarUpdate);
Now the retrieve I do not seem to have a problem with at all. It's looking like it's providing the information; the update, however, is not working.
The CarUPDRequest object is defined with different classes and one of those is a list of cars.
The class is constructed much the same as the CarUpdateInfo and that seems to work.
On the client, I know I can would call the update. But I construct the object CarUPDRequest on the client.
I have the service reference namespace like CarService. I can actually type CarService. (and get the list of class methods like the CarInfo and CarUPDRequest.
A couple of things I noticed is like the .Add for a collection defined by a list. On the client app, I DO NOT get the Add. However, if I try the same thing local on the CarService.cs, it will allow me to do the add:
Example:
CarUpd.Cars.car.add doesn't work on the client but does work on the server. Work as in is an option.
When using something like
var CarUpd = new CarUpdRequest();
Is there something I am missing here?
Any assistance on this would be greatly appreciated.
I solved my issue by doing a List CarUpd = new List();

Customize table names ASP MVC 4

I have a few tables previously created in my database that I want to map to a few model classes:
"SISTEMA.Voyages" => public class Voyage
"SISTEMA.Puerto" => public class Port
I understand that in ASP.MVC 4 with Entity framework this can be done either of two ways. However for both of them I am getting errors which I do not know how to resolve.
The first in Voyage.cs:
[Table("SISTEMA.Voyages")]
public class Voyage
Or the second in Context.cs:
public class ApplicationEntities : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Voyage>().ToTable("SISTEMA.Voyages");
}
}
For the first version I get this error when I previously assumed this was something automatic:
The type or namespace name 'Table' could not be found (are you using directive or an assembly reference?)
The type or namespace name 'TableAttribute' could not be found (are you using directive or an assembly reference?)
Fore the second I get this error which I didn't expect because I assumed this was a configuration issue and leaves me really confused:
The model backing the 'ApplicationEntities' context has changed since the database was created. Consider using Code First Migrations to update the Database.
Where is this history even recorded?
For the record, I am used to dealing with this sort of issue in Rails by typing in:
class Voyage
self.table_name = "SISTEMA.Voyages"
end
And I am not very familiar with C# / ASP.NET. Just publishing what I am looking up for the next hour unless somebody tells me where I am going wrong first.
For the first way, you are missing a using directive for the Table attribute, add this to your class:
using System.ComponentModel.DataAnnotations;
For the second way, I'm guessing that something has changed in your model and now it recommends using Code First to update the database. When you run your application, and it first hits the database, it verifies that your models match the schema - if not it can give you this error.
I recommend checking out some of these links for help getting started with EF / Code First / Migrations - they are really handy
EF + Code First
Handling Many to Many with Payload
How to work with Migrations
MSDN EF Site
Please Add:
using System.ComponentModel.DataAnnotations.Schema;

How to get Doctrine2 entity identifier without knowing its name

I am attempting to create an abstracted getId method on my base Entity class in Symfony2 using Doctrine2 for a database where primary keys are named inconsistently across tables.
When inspecting entity objects I see there is a private '_identifier' property that contains the information I am trying to retrieve but I am not sure how to properly access it.
I'm assuming there is some simple Doctrine magic similar to:
public function getId()
{
return $this->getIdentifier();
}
But I haven't managed to find it on the intertubes anywhere.
You can access this information via EntityManager#getClassMetadata(). An example would look like this:
// $em instanceof EntityManager
$meta = $em->getClassMetadata(get_class($entity));
$identifier = $meta->getSingleIdentifierFieldName();
If your entity has a composite primary key, you'll need to use $meta->getIdentifierFieldNames() instead. Of course, using this method, you'll need access to an instance of EntityManager, so this code is usually placed in a custom repository rather than in the entity itself.
Hope that helps.

C# WCF data contract partial class - new field in 2nd partial class not being picked up

I have a WCF service in which I have some data contracts. I'm using web service software factory, which uses a designer to create all the message and data and other contracts, and it creates them as partial classes. When the code is regenerated the classes are recreated.
using WcfSerialization = global::System.Runtime.Serialization;
[WcfSerialization::CollectionDataContract(Namespace = "urn:CAEService.DataContracts", ItemName = "SecurityItemCollection")]
public partial class SecurityItemCollection : System.Collections.Generic.List<SecurityItem>
{
}
The data contract is a generic List of a custom class which was working fine. However I now want to add a property to this class, so I added this partial class in the same namespace:
public partial class SecurityItemCollection
{
public int TotalRecords { get; set; }
}
This seems to work fine on the service side, but when I compile and then update the service reference from the client, the class doesn't have the new property i.e. when it serialises it and recreates it on the client side, it's missing the new property. Anyone know why this is?
TIA
EDIT:
Ok this is officially driving me nuts. The only thing I can see is that it is using the CollectionDataContract attribute instead of DataContract. Does this somehow not allow data members in the class to be serialised? Why would that be? It is working fine on the service side - I can see and populate the values no problem. However when I update the service refernce on my client there's nothing, just the colelction class without the data member.
Try to add the DataMember attribute to the TotalRecords property
Ok after much searching I finally found out that this isn't allowed i.e. classes marked as CollectionDataContract can't have data members. Why this is I have no idea but it cost me several hours and a major headache. See link below:
WCF CollectionDataContract and DataMember
I know this is old but it kept coming up when I searched on this subject.
I was able to get this working by adding a "DataMemberAttribute" attribute to the property. Below is a code example.
public partial class SecurityItemCollection
{
[global::System.Runtime.Serialization.DataMemberAttribute()]
public int TotalRecords { get; set; }
}

Stored Proc in RIA Services

I want to load some data with a SP.
I've put a SP in a Linq to SQL Class and I don't know how to use it for loading it's data in a datagrid.
In LinqToSqlDomainService I can't figure out how to call a SP.
What steps should I use.
Any samples of this ? All samples use a table.
Thank's
This post should hopefully be of help:
http://blogs.msdn.com/brada/archive/2009/08/24/business-apps-example-for-silverlight-3-rtm-and-net-ria-services-july-update-part-24-stored-procedures.aspx
You can create empty view with the same structure of your sproc and map that stored procedure to your function in your DomainService
See sample on http://cid-289eaf995528b9fd.skydrive.live.com/self.aspx/Public/sproc.zip
I found the following excellent step-by-step guide at this site -
http://betaforums.silverlight.net/forums/p/218383/521023.aspx
1) Add a ADO Entity Data Model to your Web project; Select generate from database option; Select your Database instance to connect to.
2) Choose your DB object to import to the Model. You can expand Table node to select any table you want to import to the Model. Expand Stored Procedure node to select your Stored Precedure as well. Click Finish to finish the import.
3) Right click the DB model designer to select Add/Function Import. Give the function a name (same name as your SP would be fine) and select the Stored Procedure you want to map. If your SP returns only one field, you can map the return result to a collection of scalars. If your SP returns more than one field, you could either map the return result to a collection or Entity (if all the field are from a single table) or a collection of Complex types.
If you want to use Complex type, you can click Get Column button to get all the columns for your SP. Then click Create new Complex type button to create this Complex type.
4) Add a Domain Service class to the Web project. Select the DataModel you just created as the DataContext of this Service. Select all the entitis you want expose to the client. The service functions should be generated for those entities.
5) You may not see the Complex type in the Entity list. You have to manully add a query function for your SP in your Service:
Say your SP is called SP1, the Complex type you generated is called SP1_Result.
Add the following code in your Domain Service class:
public IQueryable<SP1_Result> SP1()
{
return this.ObjectContext.SP1().AsQueryable();
}
Now you can compile your project. You might get an error like this: "SP1_Result does not have a Key" (if you not on RIA service SP1 beta). If you do, you need to do the following in the service metadata file:
Added a SP1_Result metadata class and tagged the Key field:
[MetadataTypeAttribute(typeof(SP1_Result.SP1_ResultMetadata))]
public partial class SP1_Result
{
internal sealed class SP1_ResultMetadata
{
[Key]
public int MyId; // Change MyId to the ID field of your SP_Result
}
}
6) Compile your solution. Now you have SP1_Result exposed to the client. Check the generated file, you should see SP1_Result is generated as an Entity class. Now you can access DomainContext.SP1Query and DomainContext.SP1_Results in your Silverlight code. You can treat it as you do with any other Entity(the entity mapped to a table) class.
Calling a stored procedure is trivial. Import it as a function and then invoke the function as a member of the DDS. The return value is an ObservableCollection<> that you can use to set up the DataContext of the object you want to bind.
...unless you want to use it in a Silverlight RIA app via the magic code generated proxy, in which case your goose is cooked unless your result rows exactly match one of the entities. If you can meet that criterion, edit the DomainService class and surface a method that wraps the ObjectContext method.