I have created a portable class library called DataContracts that contains my projects Messages and Views. Standard stuff like GetStockItemByIDRequest and StockView are contained in it.
The problem lies when I attempt to add DataAnnotations by using System.ComponentModel.DataAnnotations for some of my Views as such.
[DataContract]
public class StockView
{
[Required]
[DataMember]
public Guid StockID { get; set; }
[Required]
[DataMember]
public string Name { get; set; }
}
I can successfully add the System.ComponentModel.DataAnnotations to my Portable Class Library project and can successfully reference it in my Windows Phone 8 app and can even create a new instance of my view as such StockView View = new StockView(); within my Windows Phone App BUT if I try to use either Newtonsoft.Json or System.Net.Http.HttpClient by doing something like
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://myservice.com");
T result = await response.Content.ReadAsAsync<T>();
OR
T result = await Newtonsoft.Json.JsonConvert.DeserializeObjectAsync<T>("{}");
ie: where deserialization is involved...
I am faced with the error Could not load file or assembly 'System.ComponentModel.DataAnnotations, Version=2.0.5.0'. Which I assume is because it doesn't appear that System.ComponentModel.DataAnnotations is supported in Windows Phone 8 (but then why can I add it as a reference to my PCL?).
So my questions are, why isn't this error invoked when I create a new instance of these classes directly and secondly how do I work around this?
Unfortunately DataAnnotations is not currently portable. While a bit complicated, you can probably work around that by writing your own DataAnnotation attributes in a PCL, and creating an assembly with the same name for .NET Framework projects which type-forwards the attributes to the "real" versions. See this answer for some more details on this.
OK, so it turns out that my original assumptions were completely wrong. You absolutely can reference the System.ComponentModel.DataAnnotations namespace from a Windows Phone 8 project.
Basically it comes down to counterintuatively referencing the silverlight version of the dll which can be located in C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\System.ComponentModel.DataAnnotations.dll
For more information about how to build portable class libraries I suggest referring to this article .
Data annotations is supported in certain PCL profiles.
Supported profiles:
.NET 4.0.3 and up
Windows Store 8 and up
Silverlight 4 and up
Most notably, the latest Windows Phone is not supported (8.1 at the time).
See full PCL feature table in:
http://msdn.microsoft.com/en-us/library/gg597391%28v=vs.110%29.aspx
1) The process of create a new class instance doesn't involve read custom attributes, that are loaded by reflection.
2) The System.ComponentModel.DataAnnotations is exclusive for ASP.NET
The System.ComponentModel.DataAnnotations namespace provides attribute
classes that are used to define metadata for ASP.NET MVC and ASP.NET
data controls.
The portable version of System.ComponentModel.DataAnnotations seems incomplete (eg no MaxLengthAttribute).
There is this library:
https://github.com/ryanhorath/PortableDataAnnotations:
Install-Package Portable.DataAnnotations
Your PCL needs to target Silverlight 8, otherwise you will get multiple class definition errors.
Related
Hi I've created a new solution on .net 6 on VS2022 , and now I need to create reports but it looks like there is no way in vs2022. is there a way to use reporting services RDLC files in WPF on VS2002 ?
Mean a while I've open the solution on VS2009 in order to use it for building RDLC reports, but when I try to associate the data source to an Object I got this error
My object is a Class in a referenced project (class library .net6) the class is:
public class prueba
{
public int MyProperty { get; set; }
public int prop2 { get; set; }
public int prop3 { get; set; }
}
There is now a RDLC Report Designer Add-In for Visual Studio 2022.
To use those reports in your application, you need an RDLC renderer (aka "Microsoft ReportViewer"), which is available for .NET 4.0-4.8 as a nuget package:
Microsoft.ReportingServices.ReportViewerControl.Winforms
Microsoft.ReportingServices.ReportViewerControl.WebForms
Do note, though, that Microsoft did not release a ReportViewer for .NET Core or .NET 5+, and they have announced that do not intend to release one in the future (see, for example, the comments on this blog post).
There are some enthusiast ports/recompilations floating around on github and nuget, but they are not from Microsoft, nor has Microsoft given them permission to do that, so using those packages carries a certain amount of technical and legal risk. That's not something you want to do in a commercial project.
You only can use projects with netstandard, 2.0 or 2.1
so the class you will use as a data source only can be in a project using .NetFramework or netstandard.
For a project I am working on we have written a Portable PCL to contain models and other code that will be shared between multiple projects. The Data Access Layer is in another project in the solution, but I would like to use EF data annotations in the PCL to match our current convention. I have data annotations in the System.ComponentModel.DataAnnotations namespace working just fine, but System.ComponentModel.DataAnnotations.Schema annotations are not found.
The reference is added, and I have even tried re-adding EF to the PCL, but I haven't had any luck. I read another post where they ran into an issue using both namespaces simultaneously, but I can't even get .Schema working by itself.
Here is an example of what my code looks like:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; //VS shows that this is not being used
namespace Shared.Models.User
{
public class BaseUser
{
[JsonProperty("id")]
[Key]
[Column("UserId", Order = 1)] // ***ColumnAttribute not found
public Guid Id { get; set; }
...
[JsonProperty("fullName")]
[NotMapped] // ***NotMappedAttribute not found
public string FullName => String.Format("{0} {1}", FirstName, LastName);
...
}
}
The PCL is targeting .Net 4.5, .Net Core 1.0, UWP, and Xamarin platforms. In the Nuget Package manager it shows that both of the before-mentioned namespaces have been added to the project, but they do not show up in the solution explorer. I have tried cleaning the project, building, and restoring packages but still nothing. I don't see any reason that this shouldn't work in a Portable PCL, but am I missing something?
Does installing the System.ComponentModel.Annotations NuGet package help? If not, they may just not be available on that type of project.
I am trying to integrate the WeifenLuo.WinformsUI.Docking assembly into my application. To rule out external factors I created a new .Net 4 Winforms app and reference the DLL. Works fine.
I then created a .Net 4 Class Library and referenced the DLL. This this doesn't work and as soon as I try to use anything in that Docking namespace it won't compile.
To recap
C# Exe ---reference--> WeifenLuo.WinFormsUI.Docking.DockPanel.dll // OK
C# Class library ---reference--> WeifenLuo.WinFormsUI.Docking.DockPanel.dll // Not OK
I also have the WeifenLuo source and confirmed it's a class library referencing the same version of .Net. I tried adding a class library in their sample solution and referencing the WinForms project directly (not the result in assembly) and it still isn't linking properly.
Updating with Screenshot
You can't declare a private variable Inside a namespace.
Try the following code
namespace Test
{
public class Class1
{
private WeifenLuo.WinFormsUI.Docking.DockPanel dockPannel;
}
}
And if it still doesn't work, provide the error message.
I created a seperate project and copied over my models to this project.
I referenced
using System.ComponentModel.DataAnnotations
My models are defined like:
public class File
{
[Key]
[Column("file_id")]
public int Id { get; set; }
[Column("user_id")]
public int UserId { get; set; }
...
}
The error I get is:
The type or namespace Column could not be found ...
If I add a reference to EntityFramework it works fine, but I want to re-use my models project by referencing it for my Web Api Rest Client library and don't want to bring in EF for no reason.
I am using .net 4.0
Did they fix this issue (making it loosly coupled with EF) in 4.5?
Yes, this issue is fixed with .NET 4.5.
The Object Catalogue for the EntityFramework.dll of EF 5 with .NET 4.0 (also known as EF 4.4) shows all the attributes in the System.ComponentModel.DataAnnotations and System.ComponentModel.DataAnnotations.Schema namespace that are in the EF assembly:
Whereas the EntityFramework.dll of EF 5 with .NET 4.5 does not contain those namespaces anymore:
The attributes have been moved into the .NET 4.5 framework assembly System.ComponentModel.DataAnnotations.dll under the same namespace they had been before in the EF assembly:
(MinLength and MaxLength are in System.ComponentModel.DataAnnotations as well, the total list of all attributes in there is too long for the screenshot.)
You shouldn't need to reference all of EF. You should just be able to reference System.ComponentModel.DataAnnotations.dll. Adding EF may just add that as a reference for you.
FYI MSDN will tell you which namespace and assembly a framework class exists in.
You don't have to use attributes to create metadata. The whole concept of POCO models is just about it.
The trick is to have an assembly of model classes which do not reference the EF. You can safely reuse this assembly in any context.
Then, in another assembly, you create your DbContext class, refrence the EF and create the metadata programatically. In EF this approach is called Code First Fluent Api.
http://codefirst.codeplex.com/
Is it possible to reference the System.Data.Linq in a Portable Class Library project?
Note:
I am just trying to share code between a WP8 and WinStore8 app [DataContext]
No it is not. The Data namespace is unavailable in PCLs.
You can tell because http://msdn.microsoft.com/en-us/library/system.data.aspx none of its members have the PCL icon, and it is not listed on http://msdn.microsoft.com/en-us/library/vstudio/gg597391%28v=vs.100%29.aspx