问题
I have a Silverlight 4 application where I am making use of shared classes. In the .Web project, I have a class called "X.Shared.cs". This class has three string properties. When I build the application, it gives an error saying "The type 'X' already contains a definition for 'Y'". It seems that the properties in the generated code in the Silverlight Application are being seen as duplicates. I have tried cleaning my solution and rebuilding, this helps every now and then but is totally inconsistent.
Has anyone had experience in this issue? Am I perhaps doing something wrong?
The code for the shared class (X.Shared.cs) is as follows:
public partial class VideoItem
{
[Key]
public String ID
{
get;
set;
}
public String ImageURL
{
get;
set;
}
public String URL
{
get;
set;
}
}
Here is a screenshot of the solution explorer showing the generated shared file:
回答1:
To solve this problem, I created a blank .shared.cs class containing no properties (this is obviously a partial class). I then created another partial class in the same namespace with that class name and in here I put all the properties I needed to access.
回答2:
There is a limitation in the code generation. The to avoid generating duplicate members in Shared or Linked files the members can not use Auto-Implemented Properties. So to work around this you just have to define your own get and set. Keep in mind there should be a good reason not to let the RIA code generation create the members. To let RIA create the members, simply don't link or share your entity with the client but expose it through a Query method.
From http://msdn.microsoft.com/en-us/library/ee707359%28VS.91%29.aspx
Avoiding Duplicated Members
When generating an entity proxy class, it is possible that the same type and member have already been defined in the client project by using partial types. You may have defined the member in shared code or in code that only exists in the client project. RIA Services checks the existing members before generating the proxy class. Any member that is already defined will not be generated in the proxy class. [Not included - Entity members must not use Auto-Implemented Properties. Shared and Linked files from server to client project is the only method to avoid generating the member.]
回答3:
Have you checked that the class definition in X.Shared.cs is a partial?
i.e.
public partial class MyEntity
{
...
}
回答4:
Did you manually add a reference to the X.shared.cs files in your Silverlight app?
The copy is done automatically by web/client projects that are connected by RIA services. The files wind up in a hidden Generated_Code folder under your client app.
Turn on hidden files and see if you basically have the same file included twice in the Silverlight application.
来源:https://stackoverflow.com/questions/3768779/silverlight-shared-classes-in-ria-service-causing-build-errors-the-type-x-alr