How to make a field unique ASP.NET MVC5 - c#

I want to make Email field unique. It's a field of ApplicationUser (of IdentityUser) That's what I did :
namespace BidProject.Models
{
public class ApplicationUser : IdentityUser
{
public int UserID { get; set; }
[Index(IsUnique = true)]
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
I have these references:
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
I get these errors for that :
The type or namespace name 'Index' could not be found (are you missing
a using directive or an assembly reference?)
The type or namespace
name 'IndexAttribute' could not be found (are you missing a using
directive or an assembly reference?)
How can I make that field unique? Or can you tell me how IdentityUser's UserName field is unique? Thanks.

Add this:
using System.ComponentModel.DataAnnotations.Schema;
If that doesn't work, check if you have a reference to EntityFramework.dll.

You should specify a name for the Index. Use the below syntax.
[Index("IX_EmailUnique", 1, IsUnique = true)]

Looks like you are missing a namespace at the top of the file, or a project reference to EntityFramework.dll? Try putting your cursor inside the word 'Index', then hit ctrl-. -- you should see a popup menu offering something like 'resolve reference' or 'add using for using System.ComponentModel.DataAnnotations.Schema;'
If you've got, say, a .net 4.0 project referencing a .net 4.5 assembly, sometimes that'll break things. You'll need to upgrade the project to a more recent version of the framework.

Related

Getting error in Asp.net core when implementing TodoList service

I'm following on some tutorial which creates TodoList and uses multiple layer structure to handle different business logic. When I copy code from the book it shows me an error when I fire dotnet run command in windows cmd. The error I get is displayed bellow so I can specify where lies the problem but don't know how to fix this. What can I do to make this work ? I'm using dotnet 3.0.100 version.
Services\FakeTodoItemService.cs(21,17): error CS0117: 'FakeTodoItemService' does
not contain a definition for 'Title' [C:\dotnetproject\AspNetCoreTodo\AspNetCor
eTodo\AspNetCoreTodo.csproj]Services\FakeTodoItemService.cs(22,17): error CS0117: 'FakeTodoItemService' doesn not contain a definition for 'DueAt' [C:\dotnetproject\AspNetCoreTodo\AspNetCor
eTodo\AspNetCoreTodo.csproj]
And here is a code which I think may be responsible for this error. Basically I have a service that implements interface which uses a model.
FakeTodoItemService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AspNetCoreTodo.Data;
using AspNetCoreTodo.Models;
namespace AspNetCoreTodo.Services
{
public class FakeTodoItemService : ITodoItemService
{
public Task<TodoItem[]> GetIncompleteItemsAsync()
{
var item1 = new TodoItem
{
Title = "Learn ASP.NET Core",
DueAt = DateTimeOffset.Now.AddDays(1)
};
var item2 = new FakeTodoItemService
{
Title = "Build awesome apps",
DueAt = DateTimeOffset.Now.AddDays(2)
};
return Task.FromResult(new[] {item1, item2});
}
}
}
TodoItem.cs
using System;
using System.ComponentModel.DataAnnotations;
namespace AspNetCoreTodo.Models
{
public class TodoItem
{
public Guid Id {get; set;}
public bool IsDone {get; set;}
[Required]
public string Title {get; set;}
public DateTimeOffset? DueAt {get; set;}
}
}
ITodoItemService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AspNetCoreTodo.Models;
namespace AspNetCoreTodo.Services
{
public interface ITodoItemService
{
Task<TodoItem[]> GetIncompleteItemsAsync();
}
}
In FakeTodoItemService.GetIncompleteItemsAsync you have this line var item2 = new FakeTodoItemService. FakeTodoItemService should be TodoItem.

DataTypeAnnotations MetadataType not working as expected

I try to implement Metadatatype, in order to seperate Validation attributes from my Acquisitiecode class, into the AcquisitiecodeAnnotations class.
Now when I add attributes (like Required, StringLength and so on) to the Acquisitiecode class, validation works as expected. When I move these attributes to the AcquisitiecodeAnnotations class and bind this class using the MetadataType attribute, I does not work.
Please find the code examples below (I've stripped them down for readability). Also, the project is an ASP.NET Core 3.0 web application. All code, including the examples are also running in.NET Core 3.0 projects.
Snippet 1:
using System;
using System.ComponentModel.DataAnnotations;
namespace Shared.Entities
{
[MetadataType(typeof(AcquisitiecodeAnnotations))]
public partial class Acquisitiecode
{ }
public partial class AcquisitiecodeAnnotations
{
[StringLength(4, ErrorMessage = "The value cannot exceed 4 characters. ")]
public string Acquisitiecode1 { get; set; }
}
}
Snippet 2:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Shared.Entities
{
public partial class Acquisitiecode
{
public Acquisitiecode()
{
Lidmaatschap = new HashSet<Lidmaatschap>();
}
public string Acquisitiecode1 { get; set; }
public virtual Lid Lid { get; set; }
public virtual ICollection<Lidmaatschap> Lidmaatschap { get; set; }
}
}
As of October 2020 the current version of Blazor does not support Metadatatype.
For more information please read this issue.

Class Library (Portable for iOS, Android and Windows)

I have built a Class Library (Portable for iOS, Android and Windows) in VS2015.
I created a public class that having a data type with DataTable.
public class PNAWcfData
{
public int CmdTimeout { get; set; }
public string ErrMsg { get; set; }
public DataTable DTResult { get; set; }
public string XmlResult { get; set; }
}
However, I got error when build the solution.
“The type or namespace name 'DataTable' could not be found (are you
missing a using directive or an assembly reference?)”
I tried to add the using System.Data but still getting the same error. Also tried to add from Project->Reference, but it is telling that component is already automatically referenced by the build system.
Any idea why I am getting this error?
DataTable classes are not available in the PCL. This is a duplicate of Can you use DataSet and DataTables in a Portable Class Library
If you are using Xamarin, you may consider switching to Shared Project instead of PCL.

Migrations.cs is not created successfully

I am creating new module called "sms" in orchard cms using webmatrix. I create it successfully but when i generate "migrateions.cs", it doesn't generated successfully.
my sms.cs class in Model is given below
using System.Collections.Generic;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
namespace SMSs.Model{
public class smsrecord:ContentPartRecord
{
public virtual int ID{get;set;}
public virtual string Name{get;set;}
public virtual char is_deleted{get;set;}
}
public class smspart:ContentPart<smsrecord>
{
[Required]
public int ID
{
get{return ID=Record.ID;}
set{Record.ID=value;}
}
public string Name
{
get{return Name=Record.Name;}
set{Record.Name=value;}
}
public char is_deleted
{
get{return is_deleted=Record.is_deleted;}
set{Record.is_deleted=value;}
}
}
and the generated Migrations.cs class is as follow
using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
namespace sms {
public class Migrations : DataMigrationImpl {
public int Create() {
return 1;
}
}
}
the "migrations.cs" is not generated successfully why?? Please help
Class itself is generated properly, although it lacks code for creating appropriate tables because you didn't adhere to naming conventions for your record class.
Data migration code generation requires you to follow several conventions in order for it to work properly. I.e.:
Namespace of a record must end with .Models or .Records
There has to exist a public property named Id
All properties have to be virtual (required by NHibernate anyway)
Class cannot be sealed
Class cannot be abstract
Class has to implement IContent or be a subclass of ContentPartRecord
In your case, the namespace (should end with .Models) and incorrect casing of ID (should be Id) are the culprits.

Why a class in App_Code/BLL is not accessible in pages?

Hi I have a class in folder App_Code/BLL/Countries.cs ,which is a public class.
But when I tried to access the class in a ASP.NET page like:
Countries MyCountries = new Countries();
its showing error:
"The type or namespace name 'Countries' could not be found (are you missing a using directive or an assembly reference?)"
Ok your countries class will look something like this
namespace BGridNDynamicPage1
{
public class Countries()
{
......
}
}
and your code above will be something like this:
using System.Web;
etc.
namespace BGridNDynamicPage
{
public class something() : Page
{
Countries MyCountries = new Countries();
}
}
change your second class too:
using BGridNDynamicPage1;
using System.Web;
etc.
namespace BGridNDynamicPage
{
public class something() : Page
{
Countries MyCountries = new Countries();
}
}
See http://msdn.microsoft.com/en-us/library/sf0df423.aspx

Categories

Resources