In EF6, is there a annotation that will allow empty string but not allow null?
I've tried [Required]. But this annotation does not allow empty string or null.
[Required]
[MaxLength(80)]
public string ShortDescription { get; set; }
Looks like I just need to add the (AllowEmptyStrings = true) option to the [Required] annotation.
[Required(AllowEmptyStrings = true)]
[MaxLength(80)]
public string ShortDescription { get; set; }
Related
I am using .net core 3.1
public class Person
{
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string FirstName { get; set; }
[Required]
[MaxLength(50)]
public string LastName { get; set; }
[Required]
public DateTime DateOfBirth { get; set; }
**[Column(TypeName = "jsonb")]**
public IList<Address> Addresses { get; set; }
}
public class Address
{
public string Type { get; set; }
public string Company { get; set; }
public string Number { get; set; }
public string Street { get; set; }
public string City { get; set; }
}
But, when I ran add migration command that generating new table for Addresses, that I don't want
I just want to capture address list in jsonb format but not in separate table.
I am using fluent api for configuration (just for understanding above class is decorated), do we have any property where I can specify to not generate new table for any object type property.
I tried - [NotMapped] but this ignore that specific column entirely
Thank you!
Entity property look like this:
[Display(Name = "Email")]
public string Email { get; set; }
its for English,
when user select another language this attribute will change at runtime
[Display(Name = "DisplayAnotherSomething")]
public string Email { get; set; }
I have an .EDMX with my models, from EF 6.0, and I want to add attributes to some of my fields. I've read many examples where they use DataAnnotations with MetadataType... I've tried to implement it, but it does not override... For example if I have
[Queryable]
public string Name;
it will not work.
but if I have
[Queryable]
public string Name2;
It will work and I will see Name2 as part of the attributes!
The code I use in order to find those attributes is as follow :
var properties = typeof(TEntity).GetProperties().Where(prop => prop.IsDefined(typeof(QueryableAttribute), false));
Like I said, when I have Name2, i can find it in the attributes list. And when I have Name, I don't ...
here is my 3 files, they are both in "MMS.Entities" namespace
AreaMetadata.cs
namespace MMS.Entities
{
[MetadataType(typeof(AreaMetadata))]
public partial class Area
{}
public class AreaMetadata
{
[Queryable]
public string Name;
[Queryable]
public string Abbreviation;
[Queryable]
public string Description;
}
}
Area.cs
namespace MMS.Entities
{
using MMS.Common.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Area : Entity, IEntity
{
public Area()
{
this.Plants = new HashSet<Plant>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Abbreviation { get; set; }
public string Description { get; set; }
public bool IsActive { get; set; }
public bool IsDeleted { get; set; }
public int UserCreatedId { get; set; }
public Nullable<int> UserModifiedId { get; set; }
public System.DateTime DateCreated { get; set; }
public Nullable<System.DateTime> DateModified { get; set; }
public virtual ICollection<Plant> Plants { get; set; }
}
}
Should the name of AreaMetadata.cs be different? Should I include anything somewhere in order to make them both work together?
Thanks for your advices!
I believe a similar question has been answered here.
When you try to access a MetadataType property attribute, you have to get to the MetadataType using reflection, and not simply the class that uses the MetadataType. There is an explanation in the link I provided and an example of how to get it.
Basically, use reflection to get the AreaMetadata properties, not the Area properties.
Try:
MetadataTypeAttribute metadata = typeof(TEntity)
.GetCustomAttributes(typeof(MetadataTypeAttribute), true)
.OfType<MetadataTypeAttribute>().ToArray().FirstOrDefault();
PropertyInfo[] properties = metadata.MetadataTypeClass.GetProperties()
.Where(prop => prop.IsDefined(typeof(Queryable), false));
Code:
public class RegistrationViewModel {
[Required]
[StringLength(50, MinimumLength = 5)]
public String Password { get; set; }
[Required]
[Compare("Password")]
public String ComfirmPassword { get; set; }
}
The ValidationResult produced by CompareAttribute has an empty MemberNames. I would expect MemberNames to include at least "ConfirmPassword".
Right now I'm taking special action for ValidationResults that can't be linked back to a property but it would be better in this case if it could be linked back.
Do I have to write a custom CompareAttribute?
Suppose I have these two POCOs:
public class Album {
public int ID { get; set; }
[Required]
public string Title { get; set; }
public short Rating { get; set; }
public int ArtistID { get; set; }
[Required]
public virtual Artist Artist { get; set; }
}
public class Artist {
public int ID { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Album> Albums { get; set; }
}
and then I execute some code like this:
using (PickContext db=new PickContext()) {
Album pick=db.Albums.SingleOrDefault(a => a.ID==pickID);
if (pick==null) return;
pick.Rating=4;
db.SaveChanges();
I was surprised that I got a validation exception like this:
Property: "Artist", Error: "The Artist field is required."
When I changed my query to include the Artist:
Album pick=db.Albums.Include("Artist").SingleOrDefault(a => a.ID==pickID);
I no longer got the exception. If I don't tell EF to populate all properties, and they're not required, will it simply overwrite these FKs in the database? I would have thought that if I retrieve an entity and don't assign a property, the property won't be changed in the database. Is this not true?
You don't need to use the required attribute for the Artist. It simply telling to EF your navigation property is always required to be there. Since you have defined,
public int ArtistID { get; set; }
as not nullable the ArtistID will be required in the in the database level (I think that is what you expected from the required attribute here). I think you can just remove the required attribute and then this should be working as expected.