Why can't I use the Column data annotation with Entity Framework 5? - visual-studio-2010

I want to use the Column data annotation as shown in the sample code below but the compiler (and also IntelliSense) do not seem to know that particular data annotation. I'm using EF 5 in Visual Studio 2010. I installed EF 5 using NuGet. The Required and MaxLength annotations are working.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Model
{
public class Destination
{
public int DestinationId { get; set; }
[Required]
public string Name { get; set; }
public string Country { get; set; }
[MaxLength(500)]
public string Description { get; set; }
[Column(TypeName="image")]
public byte[] Photo { get; set; }
public List<Lodging> Lodgings { get; set; }
}
}
What am I missing?

Column is in:
using System.ComponentModel.DataAnnotations.Schema;
the following code:
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace ConsoleApplication2
{
public class MyContext : DbContext
{
public IDbSet<Entity> Entities { get; set; }
}
public class Entity
{
public int Id { get; set; }
[Column(TypeName = "image")]
public byte[] Photo { get; set; }
}
}
produces:

Related

Cant use system.web.security and HttpContext.Current.User.Identity.Name in dot net core 6

I'm trying to migrate from ASP.NET MVC on .NET 4.5 to ASP.NET Core 6,
I can't use System.Web.Security any more.
I also can't use HttpContext.Current.User.Identity.Name. Please suggest an alternative to implement the same functionality:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Ibq.Wages.ManagementSystem.Models;
using Microsoft.AspNetCore.Http;
namespace Ibq.Wages.ManagementSystem.AppStart
{
public class CustomRoleProvider : RoleProvider
{
public int UserCompanyId { get; set; }
public string PayerQid { get; set; }
public string PayerEid { get; set; }
public bool OTPCompleted { get; set; }
public int? AuthorizationLevels { get; set; }
private PIBQWS_prodContext context = new PIBQWS_prodContext();
public override string ApplicationName { get; set; }
public CustomRoleProvider()
{
if(String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name) == false)
UserCompany(HttpContext.Current.User.Identity.Name);
}
}
}

Entity History is not working in aspnetboilerplate

I am using aspnetboilerplate and added below configuration in preintiliaze in module. I have also added data annotation Audited to my entity but still it is not working. My entity is inheriting from AuditedEntity as don't need deleted feature. Please help
Configuration.EntityHistory.IsEnabled = true; Configuration.EntityHistory.Selectors.Add(new NamedTypeSelector("Abp.AuditedEntities", type => typeof(AuditedEntity).IsAssignableFrom(type)));
I have taken reference from here Can't enable Entity History in ASP.NET Zero
Below is entity definition
[Audited]
public partial class QuestionResponse : AuditedEntity<long>
{
public long ApplicationId { get; set; }
public long QuestionId { get; set; }
public string Response { get; set; }
public string Remark { get; set; }
public bool IsActive { get; set; }
public Application Application { get; set; }
public AbpUsers CreatorUser { get; set; }
public AbpUsers LastModifierUser { get; set; }
public Question Question { get; set; }
}
AuditedEntity<long> is not assignable to AuditedEntity.
Add a selector based on the interface IAuditedEntity instead.
Configuration.EntityHistory.Selectors.Add(
new NamedTypeSelector("Abp.AuditedEntities", type =>
// typeof(AuditedEntity).IsAssignableFrom(type)));
typeof(IAuditedEntity).IsAssignableFrom(type)));
Reference
From aspnetboilerplate/aspnetboilerplate's AuditedEntity.cs:
public abstract class AuditedEntity : AuditedEntity<int>, IEntity
{
}
public abstract class AuditedEntity<TPrimaryKey> : CreationAuditedEntity<TPrimaryKey>, IAudited
{
...
}

scaffolding seems not to work properly

I created a new asp.net MVC3 application (internet application), and then I added a new model with 3 classes:
public class BizCard
{
[Required]
public string BizCardID { get; set; }
[Required]
public string Name { get; set; }
public string Address { get; set; }
public List<string> PhoneNumbers { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
public BizType type { get; set; }
public List<BizService> OfferedServices { get; set; }
public string Description { get; set; }
}
public class BizType
{
public int BizTypeID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public double Price { get; set; }
}
public class BizService
{
public int BizServiceID { get; set; }
public List<BizType> AllowedBizTypes { get; set; }
public string Name { get; set; }
}
After that, I created a new controller, using the template "Controller with read/write actions and views using entity framework", I set the Model class to be "BizCard" and the data context class to be a new class which is called "BizDB". I was expecting to get a new class named BizDB that inherits from DbContext and includes 3 instances of DbSet:
DbSet<BizCard>, DbSet<BizType>, DbSet<BizService>.
In spite of that, I get the class with only one:
DbSet<BizCard>.
Am I missing something?
You are doing this using EF Code First approach.
1. So, you have to create a context class which should inherit DbContext containing required models as DbSet
2. Build the solution. Otherwise it will not be displayed at controller creation
Then you can create the controller using necessary model and its dbcontext.

Difficulty using "public virtual" variables in database creation

I am creating two tables in Visual Web Developer 2010 Express using the following code:
http://imgur.com/a/Mi2Bv (sorry, the forum will not let me, due to a new account, post pictures directly)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BarApp.Models
{
public class Drinks
{
public int DrinksId { get; set; }
public int EstablishmentsID { get; set; }
public string name { get; set; }
public decimal price { get; set; }
public string description { get; set; }
public string image { get; set; }
public virtual Establishments establishment { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BarApp.Models
{
public class Promotions
{
public int PromotionsId { get; set; }
public string name { get; set; }
public float discount { get; set; }
public int EstablishmentId { get; set; }
public int DrinkId { get; set; }
public string description { get; set; }
public virtual Establishments establishment { get; set; }
public virtual Drinks drink { get; set; }
}
}
But when I look at the created tables, the Promotions table has actual rows for the public virtual code, whereas the Drinks table does not.
I am able to have the Drinks table function the way I want elsewhere in the project, but I cannot get Promotions to behave the same way because it appears that "public virtual" is giving different results in each table.
I do not understand why my Promotions table is actually creating rows for the public virtual variables. Can someone help me understand?
In this particular situation I was not using the conventional way of referencing the Id field of another table.
I simply needed to change the following:
public int EstablishmentId { get; set; }
public int DrinkId { get; set; }
to the following:
public int EstablishmentsId { get; set; }
public int DrinksId { get; set; }
My tables were created as expected after I made this change.

How to make the entity key value user-assigned

I'm retrieving data from another database that has already created unique IDs for each "beer" entity. However, when I assign the unique key value from the remote database to a new "beer" object, it gets replaced as soon as the object is inserted into the database.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BeerRecommender.Models.ViewModels;
namespace BeerRecommender.Models
{
public class Beer
{
public Beer()
{
Created = DateTime.Now;
Updated = DateTime.Now;
}
[Key]
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Brewery Brewery { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public ICollection<City> Cities { get; set; }
public ICollection<Tag> Tags { get; set; }
public Style Style { get; set; }
}
}
I'm using the UnitOfWork pattern.
UnitOfWork.BeerRepository.Insert(beer);
UnitOfWork.Save();
As you already discovered, EF will generate values for int primary keys. To fine tune this behavior you need to mark your key with the DatabaseGenerated attribute (or you can also configure this with the the fluent api's HasDatabaseGeneratedOption method):
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }

Resources