ASP.NET vNext DataAnnotations DatabaseGenerated not working - c#

I'm trying define a model ID variable like this in ASP.NET 5:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
To support data annotations I've added the package System.ComponentModel.DataAnnotations in my project.json file like this:
"System.ComponentModel.Annotations": "4.0.10-beta-22811"
And in the model cs file I've added using System.ComponentModel.DataAnnotations.Schema;
Though I get the following error:
Error CS0433 The type 'DatabaseGeneratedAttribute' exists in both 'System.ComponentModel.Annotations, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
And I don't know how I should fix this really. I've tried to include the namespace System.ComponentModel.Annotations instead of System.ComponentModel.DataAnnotations but it seems like it doesn't exist as I get this error then:
Error CS0234 The type or namespace name 'Annotations' does not exist in the namespace 'System.ComponentModel' (are you missing an assembly reference?)
And if that namespace does not exist I don't understand how I can get the previous error which tells me that DatabaseGeneratedAttribute exists in two places.
I would really appreciate all help I can get with this.

You can just use the KeyAttribute. That should do the auto generation for you.
[Key]
public int Id { get; set; }
This attribute is available in the System.ComponentModel.DataAnnotations namespace
However, if you want to continue using the DatabaseGeneratedAttribute. The error is pretty self explanatory. It tells you that it is available in both namespaces
System.ComponentModel.DataAnnotations
System.ComponentModel.DataAnnotations.Schema
You will need to explicity state the namespace you need to use E.g.
[System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
You can always use an alias to keep the namespace short and sweet.

Please check that your project does not have reference to both version of System.ComponentModel.DataAnnotations.dll assembly.
Old version (4.0.0.0) can be included to your project by default, and do not be removed after you install package with new version (4.0.10.0).

Related

accessing files from a difference namespace c#

I have created a project structure like below
-Project.sln
--Provider.csproj
---Middleware
----Helper.cs
---Tests.csproj
----test1.cs
I am trying to access Helper.cs from test1.cs like using Provider.Middleware, but build is failing with message:
The type or namespace name 'Middleware' does not exist in the namespace 'Provider'.
Am I missing something?
Are you sure that you have Middleware as a folder in your project and something like this in your Helper class:
namespace Provider.Middleware{
…
}

Xamarin Forms UWP Missing Phone Call Manager Assembly

PROBLEM
Working through Xamarin University course XAM120. Reached a blocker in adding my IDial implementation to my UWP project. For some reason my project is not picking up the PhoneCallManager API on my system.
Error:
Error CS1069
The type name 'PhoneCallManager' could not be found in the namespace 'Windows.ApplicationModel.Calls'. This type has been forwarded to assembly 'Windows.Foundation.UniversalApiContract, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime' Consider adding a reference to that assembly.
Code:
using System.Threading.Tasks;
using Windows.Foundation.Metadata;
using Phoneword.UWP;
using Xamarin.Forms;
namespace Phoneword.UWP
{
public class PhoneDialer : IDialer
{
public Task<bool> DialAsync(string phoneNumber)
{
if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.Calls.CallsPhoneContract", 1, 0))
{
Windows.ApplicationModel.Calls.PhoneCallManager.ShowPhoneCallUI(phoneNumber, "Phoneword");
return Task.FromResult(true);
}
return Task.FromResult(false);
}
}
}
Add the following reference to your UWP project:
Don't forget to enable the Phone Dialer capabilities in UWP.
For Dependency Injection to work in Xamarin you need to tell it to couple your class with the correct interface!
So above your namespace Phoneword.UWP line place following
[assembly: Xamarin.Forms.Dependency (typeof (PhoneDialer))]
Look at the tutorial in the docs : https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/introduction/#Registration

Why is my Interface not being found?

I'm getting, "The type or namespace name 'INRBQDeliveryRepository' could not be found (are you missing a using directive or an assembly reference?)" here:
public class DeliveryController : ApiController
{
private readonly INRBQDeliveryRepository _deliveryRepository;
The interface type I'm trying to reference there is:
namespace SeaStore.Data.Legacy.Interfaces
{
public interface INRBQDeliveryRepository
. . .
These are different projects in a solution (NRBQ.Web is trying to access its sibling, SeaStore.Data.Legacy); adding "using SeaStore.Data.Legacy.Interfaces;" to the class that prevents compilation with the "not found" error doesn't help - in fact, I go from the error noted above to getting an addition error, specifically:
"The type or namespace name 'Data' does not exist in the namespace 'SeaStore' (are you missing an assembly reference?)" in the Controller class here:
using SeaStore.Data.Legacy.Interfaces;
Why is my other-project interface not visible?
The error message you received is kind of selfexplanatory: are you missing an assembly reference.
It indicates that you are actually missing a reference to an assembly.
Add --> Reference --> Locate the library where your interface is defined, and add the reference.
The using should resolve itself then, because it can now find the namespace. Which means you can use your interface!

WCF DataContractSerializer and XAML Namespaces

I am sending an object to a WCF service that contains a Windows Workflow definition, but the deserializer is faulting when trying to deserialize my custom activities.
This was previously working when I had the activities' namespaces defined in the form of:
xmlns:tta="clr-namespace:MyNamespace;assembly=MyAssembly"
but for maintainability reasons I have now mapped my activity namespaces to a XAML namespace using assembly attributes:
[assembly: XmlnsPrefix("http://schemas.product.com/activities/", "tta")]
[assembly: XmlnsDefinition("http://schemas.product.com/activities/", "MyNamespace")]
Thus my xaml namespace looks like: xmlns:tta="http://schemas.thacktech.com/activities/"
And my activity declared as: <tta:Naptime />
Because of this change, I now recieve NetDispatcherFaultException which reads:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:job. The InnerException message was 'Element 'http://schemas.datacontract.org/2004/07/System.Activities:Activity' contains data from a type that maps to the name 'Naptime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null:MyNamespace.Naptime'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'MyNamespace.Naptime' and namespace 'Naptime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.'. Please see InnerException for more details.
Questions:
Why does the deseralizer succeed with type resolution when using the clr-namespace syntax, but fail when using the url-style syntax for a namespace declaration?
It looks like the type resolver is completely misinterpreting the type, listing the class name as the namespace. Why would it do this?
How do I properly implement this?
Thanks!

Missing extension methods in HtmlHelper using NHaml

I discovered NHaml some days ago and it's a great project.
When I try to use MVC2 Html helpers like Html.LabelFor(), Html.TextBoxFor(); the views won't compile.
Example:
error CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'LabelFor' and no extension method 'LabelFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
0185: textWriter.Write(" ");
0185: textWriter.Write(Convert.ToString(Html.LabelFor(model => model.Username)));
0187: textWriter.WriteLine();
error CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'TextBoxFor' and no extension method 'TextBoxFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
0194: textWriter.Write(" ");
0194: textWriter.Write(Convert.ToString(Html.TextBoxFor(model => model.Username)));
0196: textWriter.WriteLine();
I tried to add assemblies and namespaces in the nhaml's Web.config section but it doesn't change anything.
I'm using :
System.Web.Mvc 2.0
.NET Framework 3.5 SP1
Nhaml 1.5.0.2 from git trunk (and tried other builds)
My NHaml configuration is:
<nhaml autoRecompile="true" templateCompiler="CSharp3" encodeHtml="false" useTabs="false" indentSize="2">
It looks like you have an assembly reference problem.
You are probably referencing the MVC 1.0 assemblies, instead of 2.0 assemblies?
The problem is the view class contains a non-generic HtmlHelper. Or some new extension methods requires the ViewData.Model's type.
To correct this problem, change the property and instantiation in NHaml.Web.Mvc/NHamlMvcView.cs.
//public HtmlHelper Html { get; protected set; } // line 42
public HtmlHelper<TModel> Html { get; protected set; }
//Html = new HtmlHelper( viewContext, this ); // line 37
Html = new HtmlHelper<TModel>( viewContext, this );
Rebuild and use :)
As far as I can see the new MVC helpers are not supported, actually only a limited amount of HtmlHelpers are namely LinkExtensions. As a wild guess, you can possibly try to adding the LabelExtensions to the setup of the NHaml viewengine in the NHaml.Web.Mvc/NHamlMvcViewEngine.cs file (since you do have the source) and check if that works.
private void InitializeTemplateEngine()
{
// snip
_templateEngine.Options.AddReference( typeof( LabelExtensions ).Assembly.Location ); // Line 50
}

Categories