Mon*_*nah 6 c# sql-server entity-framework
我正在使用ASP.NET MVC5和MS SQL 2008以及EntityFramework 6,在我的应用程序中,我有一个类体验,允许客户添加他们的经历从期间到期间的详细信息:
public class Experience
{
public int Id { get; set; }
public string Title { get; set; }
public string Company { get; set; }
public string FromDate { get; set; }
public string ToDate { get; set; }
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
FromDate格式将是MMM yyyy,例如2009年1月,2010年10月,等等,ToDate格式将是MMM yyyy或Present,并且应该大于或等于FromDate值,例如:
我把课程时间定为如下
public class Period: IComparable
{
public Period()
{
this.Month="Present";
}
public Period(string Month,int Year)
{
this.Month=Month;
this.Year=Year;
}
public int CompareTo(object obj)
{
if(obj==null)
return 1;
Period period = obj as Period;
if (period != null)
{
if(this.Month=="Present" && period.Month=="Present")
return 0;
else if(period.Month=="Present")
return -1;
else if(this.Month=="Present")
return 1;
else
{
DateTime date;
DateTime periodDate;
if(!DateTime.TryParse(string.Format("01 {0} {1}",Month,Year),out date))
throw new ArgumentException("Instance is not a valid Period!");
if(!DateTime.TryParse(string.Format("01 {0} {1}",period.Month,period.Year),out periodDate))
throw new ArgumentException("Object is not a valid Period!");
return date.Date.CompareTo(periodDate.Date);
}
}
else
throw new ArgumentException("Object is not a Period");
}
public override int GetHashCode()
{
if(this==null)
return 0;
else
{
DateTime date;
if(!DateTime.TryParse(string.Format("01 {0} {1}",Month,Year),out date))
throw new ArgumentException("Instance is not a valid Period!");
return date.GetHashCode();
}
}
public override bool Equals(object obj)
{
return this.CompareTo(obj)==0;
}
public static bool operator==(Period left, Period right)
{
return left.Equals(right);
}
public static bool operator!=(Period left, Period right)
{
return !(left==right);
}
public static bool operator<(Period left, Period right)
{
return left.CompareTo(right)<0;
}
public static bool operator>(Period left, Period right)
{
return left.CompareTo(right)>0;
}
public override string ToString()
{
if(string.IsNullOrWhiteSpace(Month))
return string.Empty;
else if(Month=="Present")
return Month;
else
return string.Format("{0} {1}",Month,Year);
}
public string Month{get; set;}
public int Year{get; set;}
}
Run Code Online (Sandbox Code Playgroud)
样本DEMO
在sql中我创建了以下数据类型
create type Period from nvarchar(8) not null;
Run Code Online (Sandbox Code Playgroud)
问题:
任何想法将不胜感激
这里有一个关于如何做到这一点的解决方法
你的体验课会是这样的
public class Experience
{
public int Id { get; set; }
public string Title { get; set; }
public string Company { get; set; }
public Period FromDate { get; set; }
public Period ToDate { get; set; }
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在您的周期类中更改以下内容,这将是 [ComplexType]
// change the folllowing
[NotMapped]
public string Month{get; set;}
[NotMapped]
public int Year{get; set;}
// add this property to use it for EntityFramework mapping
public string Date
{
get
{
return ToString();
}
set
{
if (!string.IsNullOrEmpty(Date))
{
if (Date == "Present")
Month = "Present";
else
{
var split = Date.Split(' ');
Month = split[0];
Year = Convert.ToInt32(split[1]);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在您的映射中,您执行以下操作
modelBuilder.Entity<Experience>()
.Property(t => t.FromDate.Date)
.IsRequired()
.HasMaxLength(8);
modelBuilder.Entity<Experience>()
.Property(t => t.ToDate.Date)
.IsRequired()
.HasMaxLength(8);
Run Code Online (Sandbox Code Playgroud)
要让实体框架将字段设置为Period数据类型,您可以这样做 1-启用迁移 2-首先添加迁移 //这将创建一个文件dbmigration,其中包含数据库和表构造 3-在函数Up中最后你使用 Sql(@""); 编写以下内容,这里我将把 sql 语句给你
create function CheckPeriod(@period nvarchar(8)) -- this function will make sure that Period is valid
returns bit
as
begin
declare @month nvarchar(3)
if ( @period is null )
return 0
if( @period = 'Present')
return 1;
set @month=substring(@period,0,3)
if(@month in ('Jan','Feb','Mar','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'))
return 1;
return isdate(@period);
end
create function CheckValidity(@right as nvarchar(8),@left as nvarchar(8)) -- this function will compare FromDate to ToDate and return valid if ToDate>FromDate
returns bit
as
begin
if(@right='Present')
return 0;
if(@left='Present' and cast(@right as date)>=getdate())
return 0;
if(@left='Present')
return 1;
if(cast(@left as date)>cast(@right as date))
return 1;
return 0;
end
exec sp_addtype 'Period','nvarchar(8)','NOT NULL'
alter table Experiences alter column FromDate Period
alter table Experiences alter column ToDate Period
alter table Experiences
add constraint FromDateIsPeriod check ( dbo.CheckPeriod(FromDate)=1)
alter table Experiences
add constraint ToDateIsPeriod check ( dbo.CheckPeriod(ToDate)=1)
alter table Experiences
add constraint PeriodValidity check ( dbo.CheckValidity(FromDate,ToDate)=1)
Run Code Online (Sandbox Code Playgroud)
在 Down 函数中,如果您希望编写代码来删除函数并更改表字段以将它们返回为 nvarchar(8) 并最终删除数据类型 period
希望对你有帮助
| 归档时间: |
|
| 查看次数: |
165 次 |
| 最近记录: |