本文整理汇总了C#中Microsoft.EntityFrameworkCore.Metadata.Internal.Model.AddEntityType方法的典型用法代码示例。如果您正苦于以下问题:C# Model.AddEntityType方法的具体用法?C# Model.AddEntityType怎么用?C# Model.AddEntityType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.EntityFrameworkCore.Metadata.Internal.Model
的用法示例。
在下文中一共展示了Model.AddEntityType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Circular_inheritance_should_throw
public void Circular_inheritance_should_throw()
{
var model = new Model();
// A
// / \
// B C
// \
// D
var a = model.AddEntityType(typeof(A).Name);
var b = model.AddEntityType(typeof(B).Name);
var c = model.AddEntityType(typeof(C).Name);
var d = model.AddEntityType(typeof(D).Name);
b.HasBaseType(a);
c.HasBaseType(a);
d.HasBaseType(c);
Assert.Equal(
CoreStrings.CircularInheritance(a, a),
Assert.Throws<InvalidOperationException>(() => { a.HasBaseType(a); }).Message);
Assert.Equal(
CoreStrings.CircularInheritance(a, b),
Assert.Throws<InvalidOperationException>(() => { a.HasBaseType(b); }).Message);
Assert.Equal(
CoreStrings.CircularInheritance(a, d),
Assert.Throws<InvalidOperationException>(() => { a.HasBaseType(d); }).Message);
}
示例2: Does_not_detect_duplicate_table_names_for_inherited_entities
public virtual void Does_not_detect_duplicate_table_names_for_inherited_entities()
{
var model = new Model();
var entityA = model.AddEntityType(typeof(A));
SetPrimaryKey(entityA);
var entityC = model.AddEntityType(typeof(C));
SetBaseType(entityC, entityA);
Validate(model);
}
示例3: Detects_duplicate_table_names
public virtual void Detects_duplicate_table_names()
{
var model = new Model();
var entityA = model.AddEntityType(typeof(A));
SetPrimaryKey(entityA);
var entityB = model.AddEntityType(typeof(B));
SetPrimaryKey(entityB);
entityA.Relational().TableName = "Table";
entityB.Relational().TableName = "Table";
VerifyError(RelationalStrings.DuplicateTableName("Table", null, entityB.DisplayName()), model);
}
示例4: Does_not_detect_duplicate_table_names_in_different_schema
public virtual void Does_not_detect_duplicate_table_names_in_different_schema()
{
var model = new Model();
var entityA = model.AddEntityType(typeof(A));
SetPrimaryKey(entityA);
var entityB = model.AddEntityType(typeof(B));
SetPrimaryKey(entityB);
entityA.Relational().TableName = "Table";
entityA.Relational().Schema = "SchemaA";
entityB.Relational().TableName = "Table";
entityB.Relational().Schema = "SchemaB";
Validate(model);
}
示例5: Constructor_throws_when_referenced_key_not_on_referenced_entity
public void Constructor_throws_when_referenced_key_not_on_referenced_entity()
{
var model = new Model();
var principalEntityType = model.AddEntityType("R");
var dependentEntityType = model.AddEntityType("D");
var fk = dependentEntityType.AddProperty("Fk", typeof(int));
var principalKey = dependentEntityType.SetPrimaryKey(fk);
Assert.Equal(
CoreStrings.ForeignKeyReferencedEntityKeyMismatch("{'Fk'}", "R"),
Assert.Throws<InvalidOperationException>(() => dependentEntityType.AddForeignKey(new[] { fk }, principalKey, principalEntityType)).Message);
}
示例6: CreateForeignKey
private ForeignKey CreateForeignKey()
{
var model = new Model();
var entityType = model.AddEntityType(typeof(E));
var idProperty = entityType.AddProperty("id", typeof(int));
var key = entityType.SetPrimaryKey(idProperty);
var fkProperty = entityType.AddProperty("p", typeof(int));
return entityType.AddForeignKey(fkProperty, key, entityType);
}
示例7: CreateModel
private static Model CreateModel(bool generateValues = true)
{
var model = new Model();
var entityType = model.AddEntityType("Led");
entityType.AddProperty("Zeppelin", typeof(Guid));
entityType.AddProperty("Stairway", typeof(Guid)).RequiresValueGenerator = generateValues;
return model;
}
示例8: BuildModel
protected override Model BuildModel()
{
var model = new Model();
var someSimpleEntityType = model.AddEntityType(typeof(SomeSimpleEntityBase).FullName);
var simpleKeyProperty = someSimpleEntityType.AddProperty("Id", typeof(int));
simpleKeyProperty.RequiresValueGenerator = true;
someSimpleEntityType.GetOrSetPrimaryKey(simpleKeyProperty);
var someCompositeEntityType = model.AddEntityType(typeof(SomeCompositeEntityBase).FullName);
var compositeKeyProperty1 = someCompositeEntityType.AddProperty("Id1", typeof(int));
var compositeKeyProperty2 = someCompositeEntityType.AddProperty("Id2", typeof(string));
compositeKeyProperty2.IsNullable = false;
someCompositeEntityType.GetOrSetPrimaryKey(new[] { compositeKeyProperty1, compositeKeyProperty2 });
var entityType1 = model.AddEntityType(typeof(SomeEntity).FullName);
entityType1.HasBaseType(someSimpleEntityType);
var property3 = entityType1.AddProperty("Name", typeof(string));
property3.IsConcurrencyToken = true;
var entityType2 = model.AddEntityType(typeof(SomeDependentEntity).FullName);
entityType2.HasBaseType(someCompositeEntityType);
var fk = entityType2.AddProperty("SomeEntityId", typeof(int));
entityType2.GetOrAddForeignKey(new[] { fk }, entityType1.FindPrimaryKey(), entityType1);
var justAProperty = entityType2.AddProperty("JustAProperty", typeof(int));
justAProperty.RequiresValueGenerator = true;
var entityType3 = model.AddEntityType(typeof(FullNotificationEntity));
entityType3.GetOrSetPrimaryKey(entityType3.AddProperty("Id", typeof(int)));
var property6 = entityType3.AddProperty("Name", typeof(string));
property6.IsConcurrencyToken = true;
entityType3.ChangeTrackingStrategy = ChangeTrackingStrategy.ChangingAndChangedNotifications;
var entityType4 = model.AddEntityType(typeof(ChangedOnlyEntity));
entityType4.GetOrSetPrimaryKey(entityType4.AddProperty("Id", typeof(int)));
var property8 = entityType4.AddProperty("Name", typeof(string));
property8.IsConcurrencyToken = true;
entityType4.ChangeTrackingStrategy = ChangeTrackingStrategy.ChangedNotifications;
var entityType5 = model.AddEntityType(typeof(SomeMoreDependentEntity).FullName);
entityType5.HasBaseType(someSimpleEntityType);
var fk5a = entityType5.AddProperty("Fk1", typeof(int));
var fk5b = entityType5.AddProperty("Fk2", typeof(string));
entityType5.GetOrAddForeignKey(new[] { fk5a, fk5b }, entityType2.FindPrimaryKey(), entityType2);
return model;
}
示例9: Creates_shadow_state_only_entry_when_entity_is_fully_shadow_state
public void Creates_shadow_state_only_entry_when_entity_is_fully_shadow_state()
{
var model = new Model();
var entityType = model.AddEntityType("RedHook");
entityType.AddProperty("Long", typeof(int));
entityType.AddProperty("Hammer", typeof(string));
var contextServices = TestHelpers.Instance.CreateContextServices(model);
var stateManager = contextServices.GetRequiredService<IStateManager>();
var factory = contextServices.GetRequiredService<IInternalEntityEntryFactory>();
var entry = factory.Create(stateManager, entityType, new Random());
Assert.IsType<InternalShadowEntityEntry>(entry);
Assert.Same(stateManager, entry.StateManager);
Assert.Same(entityType, entry.EntityType);
Assert.Null(entry.Entity);
}
示例10: Creates_mixed_entry_when_entity_CLR_entity_type_and_shadow_properties
public void Creates_mixed_entry_when_entity_CLR_entity_type_and_shadow_properties()
{
var model = new Model();
var entityType = model.AddEntityType(typeof(RedHook));
entityType.AddProperty("Long", typeof(int), shadow: false);
entityType.AddProperty("Hammer", typeof(string), shadow: true);
var contextServices = TestHelpers.Instance.CreateContextServices(model);
var stateManager = contextServices.GetRequiredService<IStateManager>();
var factory = contextServices.GetRequiredService<IInternalEntityEntryFactory>();
var entity = new RedHook();
var entry = factory.Create(stateManager, entityType, entity);
Assert.IsType<InternalMixedEntityEntry>(entry);
Assert.Same(stateManager, entry.StateManager);
Assert.Same(entityType, entry.EntityType);
Assert.Same(entity, entry.Entity);
}
示例11: Can_only_override_existing_ClrType_value_explicitly
public void Can_only_override_existing_ClrType_value_explicitly()
{
var model = new Model();
model.AddEntityType(typeof(Customer)).AddProperty(Customer.NameProperty.Name);
var modelBuilder = new InternalModelBuilder(model);
var entityBuilder = modelBuilder.Entity(typeof(Customer), ConfigurationSource.Explicit);
var builder = entityBuilder.Property(Customer.NameProperty.Name, ConfigurationSource.Convention);
Assert.Null(builder.Metadata.GetClrTypeConfigurationSource());
builder.Metadata.ClrType = typeof(string);
Assert.Equal(ConfigurationSource.Explicit, builder.Metadata.GetClrTypeConfigurationSource());
Assert.True(builder.HasClrType(typeof(string), ConfigurationSource.DataAnnotation));
Assert.False(builder.HasClrType(typeof(int), ConfigurationSource.DataAnnotation));
Assert.Equal(typeof(string), builder.Metadata.ClrType);
Assert.True(builder.HasClrType(typeof(int), ConfigurationSource.Explicit));
Assert.Equal(typeof(int), builder.Metadata.ClrType);
}
示例12: Can_add_and_remove_entity_by_type
public void Can_add_and_remove_entity_by_type()
{
var model = new Model();
Assert.Null(model.FindEntityType(typeof(Customer)));
Assert.Null(model.RemoveEntityType(typeof(Customer)));
var entityType = model.AddEntityType(typeof(Customer));
Assert.Equal(typeof(Customer), entityType.ClrType);
Assert.NotNull(model.FindEntityType(typeof(Customer)));
Assert.Same(model, entityType.Model);
Assert.NotNull(entityType.Builder);
Assert.Same(entityType, model.GetOrAddEntityType(typeof(Customer)));
Assert.Equal(new[] { entityType }, model.GetEntityTypes().ToArray());
Assert.Same(entityType, model.RemoveEntityType(entityType.ClrType));
Assert.Null(model.RemoveEntityType(entityType.ClrType));
Assert.Null(model.FindEntityType(typeof(Customer)));
Assert.Null(entityType.Builder);
}
示例13: Setting_CLR_base_for_shadow_entity_type_should_throw
public void Setting_CLR_base_for_shadow_entity_type_should_throw()
{
var model = new Model();
var a = model.AddEntityType(typeof(A));
var b = model.AddEntityType(typeof(B).Name);
Assert.Equal(
CoreStrings.NonShadowBaseType(b, a),
Assert.Throws<InvalidOperationException>(() => { b.HasBaseType(a); }).Message);
}
示例14: ForeignKeys_should_be_updated_when_base_type_changes
public void ForeignKeys_should_be_updated_when_base_type_changes()
{
var model = new Model();
var customerType = model.AddEntityType(typeof(SpecialCustomer));
var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));
var orderType = model.AddEntityType(typeof(Order));
var specialOrderType = model.AddEntityType(typeof(SpecialOrder));
specialOrderType.HasBaseType(orderType);
var derivedForeignKeyProperty = specialOrderType.GetOrAddProperty(Order.IdProperty);
specialOrderType.GetOrAddForeignKey(derivedForeignKeyProperty, customerKey, customerType);
var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey, customerType);
specialOrderType.HasBaseType(null);
Assert.Equal(new[] { new[] { Order.CustomerIdProperty.Name } },
orderType.GetForeignKeys().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
Assert.Equal(new[] { new[] { Order.IdProperty.Name } },
specialOrderType.GetForeignKeys().Select(fk => fk.Properties.Select(p => p.Name).ToArray()).ToArray());
}
示例15: Setting_base_type_throws_when_grandchild_contain_duplicate_navigation
public void Setting_base_type_throws_when_grandchild_contain_duplicate_navigation()
{
var model = new Model();
var customerType = model.AddEntityType(typeof(Customer));
var customerKey = customerType.GetOrAddKey(customerType.GetOrAddProperty(Customer.IdProperty));
var orderType = model.AddEntityType(typeof(Order));
var foreignKeyProperty = orderType.GetOrAddProperty(Order.CustomerIdProperty);
var customerForeignKey = orderType.GetOrAddForeignKey(foreignKeyProperty, customerKey, customerType);
customerForeignKey.HasDependentToPrincipal("Customer");
var specialOrderType = model.AddEntityType(typeof(SpecialOrder));
specialOrderType.HasBaseType(orderType);
var specialCustomerType = model.AddEntityType(typeof(SpecialCustomer));
var verySpecialOrderType = model.AddEntityType(typeof(VerySpecialOrder));
var derivedForeignKeyProperty = verySpecialOrderType.GetOrAddProperty(Order.IdProperty);
var property = specialCustomerType.AddProperty("AltId", typeof(int));
var specialCustomerKey = specialCustomerType.GetOrAddKey(property);
var specialCustomerForeignKey = verySpecialOrderType.GetOrAddForeignKey(derivedForeignKeyProperty, specialCustomerKey, specialCustomerType);
specialCustomerForeignKey.HasDependentToPrincipal("Customer");
Assert.Equal(
CoreStrings.DuplicateNavigationsOnBase(typeof(VerySpecialOrder).FullName, typeof(SpecialOrder).FullName, "Customer"),
Assert.Throws<InvalidOperationException>(() => { verySpecialOrderType.HasBaseType(specialOrderType); }).Message);
}