AutoFixture EF entity constraints - entity-framework

Is it possbile to configure AutoFixture so that it adheres the entity constraints [from the EDMX file]?
E.g. Consider a snippet from the CSDL section of my EDMX file:
<EntityType Name="RndtAd">
...
<Property Name="AD" Type="Decimal" Precision="12" Scale="0" Nullable="false" />
<Property Name="USERNAME" Type="String" MaxLength="255" FixedLength="false" Unicode="true" />
<Property Name="VERSION" Type="Decimal" Precision="12" Scale="4" Nullable="false" />
<Property Name="EFFECTIVE_FROM" Type="DateTime" Precision="3" />
<Property Name="EFFECTIVE_FROM_TZ" Type="DateTime" Precision="7" />
<Property Name="EFFECTIVE_TILL" Type="DateTime" Precision="3" />
<Property Name="EFFECTIVE_TILL_TZ" Type="DateTime" Precision="7" />
<Property Name="IS_TEMPLATE" Type="String" MaxLength="1" FixedLength="true" Unicode="false" />
<Property Name="IS_USER" Type="String" MaxLength="1" FixedLength="true" Unicode="false" />
<Property Name="STRUCT_CREATED" Type="String" MaxLength="1" FixedLength="true" Unicode="false" />
<Property Name="AD_TP" Type="String" MaxLength="20" FixedLength="false" Unicode="true" />
<Property Name="PERSON" Type="String" MaxLength="40" FixedLength="false" Unicode="true" />
<Property Name="TITLE" Type="String" MaxLength="20" FixedLength="false" Unicode="true" />
<Property Name="FUNCTION_NAME" Type="String" MaxLength="20" FixedLength="false" Unicode="true" />
<Property Name="COMPANY" Type="String" MaxLength="40" FixedLength="false" Unicode="true" />
<Property Name="STREET" Type="String" MaxLength="40" FixedLength="false" Unicode="true" />
...
What I would like if fixture.Create<RndtAd>() generated randomly an entity where all the previous constraints are satisfied.
What options do I have? All suggestions are welcome.
EDIT. I'm not bound to AutoFixture. If there is another tool which does the job, I'm ok with that too.

AutoFixture has no built-in support for Entity Framework, but during the last couple of years, several people have fought their own battles to integrate the two.
Here's what a Google search turned up for me:
Autofixture and Moq to test Entity Framework project
AutoFixture.AutoEntityFramework
Creating a domain model without circular references in Entity Framework
Using autofixture in my data integration tests to create proxies
How to mockup Entity Framework 6 With Moq & Autofixture
Perhaps you can find some inspiration by looking some of those resources over.

As-is, AutoFixture can't be customized through .EDMX files.

Related

Receive bytea with asp .net api

First of all, sorry for the probable English mistakes
I created a table in my postgresql database using the entity framework.
In edmx in the creation section I passed as bytea, however in the mapping I used binary (primitive type use is necessary).
<EntityType Name="styleexample">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Name="example" Type="String" MaxLength="40" />
<Property Name="example1" Type="Binary" />
<Property Name="example2" Type="DateTime" />
</EntityType>
<EntityType Name="styleexample">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="int4" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="example" Type="varchar" MaxLength="40" />
<Property Name="example1" Type="bytea" />
<Property Name="example2" Type="timestamp" />
</EntityType>
The table was created with the correct typing
In the entity I referenced the column as byte []
In the column I inserted the base64 string of a png image, but in the service when I return the value is different from what I inserted
My response is also byte[]
It works. However, it returns the incorrect value. Is there a different way to do this process?
I solved the issue by adding to the service
var example1 = new StreamReader(new MemoryStream(estilo.example1));
var example1Stream = example1 .ReadToEnd();

How to config to send mail with activiti explorer?

I am using Activiti Explorer version 5.17, I want to send mail by using Mail Task but it not work.
My config:
In engine.properties file (I can not found activiti.cfg.xml file):
engine.email.enabled=true
engine.email.host=myserver.com.vn
engine.email.port=25
engine.email.username=test
engine.email.password=test#123
My Mail Task
<serviceTask id="sid-024BFBEB-EC9D-475E-BE44-6E0996FFB64D" activiti:type="mail">
<extensionElements>
<activiti:field name="from" stringValue="test#myserver.com.vn" />
<activiti:field name="to" expression="test#myserver.com.vn" />
<activiti:field name="subject" expression="Your order has been shipped" />
<activiti:field name="html">
<activiti:expression>
<![CDATA[
<html>
<body>
Hello ,<br/><br/>
As of now, your order has been <b>processed and shipped</b>.<br/><br/>
Kind regards,<br/>
TheCompany.
</body>
</html>
]]>
</activiti:expression>
</activiti:field>
</extensionElements>
</serviceTask>
When i run process, nothing is display in console, and it not work.
Thanks for any help. :)
I tried something but in the version 5.18(I'm not sure if can be applicable on your version), try to configure those thing on activiti-custom-context.xml(placed in activiti-explorer\WEB-INF\classes). Also you can configure your db connection, for example.
First uncomment the beans and then fill your db information in the bean with the id "dataSource".
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/database" />
<property name="username" value="user" />
<property name="password" value="password" />
<property name="defaultAutoCommit" value="false" />
</bean>
To configure the email add some properties to the bean with the id "processEngineConfiguration"
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jobExecutorActivate" value="true" />
<property name="enableDatabaseEventLogging" value="true" />
<property name="customFormTypes">
<list>
<bean class="org.activiti.explorer.form.UserFormType"/>
<bean class="org.activiti.explorer.form.ProcessDefinitionFormType"/>
<bean class="org.activiti.explorer.form.MonthFormType"/>
</list>
</property>
<property name="mailServerUseTLS" value="true" /><!--This is important if you use Gmail as your hoster -->
<property name="mailServerHost" value="smtp.gmail.com" />
<property name="mailServerPort" value="587" />
<property name="mailServerUsername" value="my#host.com" />
<property name="mailServerPassword" value="hostpassword"/>
</bean>

connecting to Oracle XE with myBatis using JDBC in Eclipse

I'm using Eclipse with Maven, in myBatis-config.xml I have the following codes. The H2 part of the code works as I can connect to H2 with my program and access the database. The Oracle part of my code doesn't work. I'm using ORACLE DATABASE XE 11.2, application express with a workspace: test, username: name, password: 123. When I run a testing class in Eclipse, I could pass the H2 tests, but when I run the same test using oracle instead, it gets an error. "Error selecting key or setting result to parameter object. Case: java.sql.SQLSyntaxErrorException: ORA-02289: sequence does not exist.
<environment id="H2">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:tcp://localhost:9096/sample/testDB" />
<property name="username" value="sa" />
<property name="password" value="123" />
</dataSource>
</environment>
<environment id="ORACLE">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe" />
<property name="username" value="system" />
<property name="password" value="123" />
</dataSource>
</environment>
Hello reading the documentation from the official site of MyBatis I could obtain the following information:
In case of using the multi-db feature you will need to inform the databaseIdProvider property in the following way:
In case of using the multi-db feature you will need to inform the databaseIdProvider property in the following way:
<bean id="vendorProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="SQL Server">sqlserver</prop>
<prop key="DB2">db2</prop>
<prop key="Oracle">oracle</prop>
<prop key="MySQL">mysql</prop>
</props>
</property>
</bean>
<bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider">
<property name="properties" ref="vendorProperties"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" />
<property name="databaseIdProvider" ref="databaseIdProvider"/>
</bean>
Hope it has been helpful.
Greetings.
NOTE Since 1.3.0, configuration property has been added. It can be specified a Configuration instance directly without MyBatis XML configuration file. For example:
mybatis.org/spring/es/factorybean.html

specify a database schema for activiti-5.12.1 tables

I am going to use activiti-5.12.1, in my project.
here is the activiti.cfg.xml file:
<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration" >
<property name="databaseType" value="postgres" />
<property name="jdbcUrl" value="jdbc:postgresql://project:5432/MYPROJECT" />
<property name="jdbcDriver" value="org.postgresql.Driver" />
<property name="jdbcUsername" value="me" />
<property name="jdbcPassword" value="you" />
<property name="databaseSchemaUpdate" value="false" />
<property name="jobExecutorActivate" value="false" />
<property name="history" value="full" />
<property name="customPreVariableTypes">
<list>
<ref bean="activitiScriptNodeType" />
<ref bean="activitiScriptNodeListType" />
</list>
</property>
<property name="mailServerHost" value="mail.my-corp.com" />
<property name="mailServerPort" value="5025" />
</bean>
</beans>
I want to create activiti database on my own, by using the scripts which are available in activiti-engine-5.12.1.jar.
By default, the tables are created in public schema, but I want them to be in another schema like mySchema for example.
my questioin is how can I manage this, besides, how can I specify this in activiti.cfg.xml, to inform activiti engine api that activiti tables are in mySchema?
I am using MySQL database for activiti, so i have got some configuration like this to creat my own schema name :
<property name="databaseType" value="mysql" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti" />
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="root" />
So as you using postgresql, i am not quite sure about whether the following code will work for you or not :
<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration" >
<property name="databaseType" value="postgres" />
<property name="jdbcUrl" value="jdbc:postgresql://project:5432/MYPROJECT/activiti" />
<property name="jdbcDriver" value="org.postgresql.Driver" />
<property name="jdbcUsername" value="me" />
<property name="jdbcPassword" value="you" />
<property name="databaseSchemaUpdate" value="false" />
<property name="jobExecutorActivate" value="false" />
<property name="history" value="full" />
<property name="customPreVariableTypes">
<list>
<ref bean="activitiScriptNodeType" />
<ref bean="activitiScriptNodeListType" />
</list>
</property>
<property name="mailServerHost" value="mail.my-corp.com" />
<property name="mailServerPort" value="5025" />
</bean>
Before deploying `activiti with new changes, mke sure that your database has the schema created.
Try changing the database's type to your database's name or "postgresql".
If you are using spring the below property helps:
spring.activiti.databaseSchema=MY_SCHEMA

Entity Framework: 0..1-to-many foreign key constraint not recognized?

I am using EF4 to map a DB schema to an object model; I initially generated the EDMX from the database but have been editing the XML directly (trying to leave the SSDL the same while changing the CSDL/MSL to approximate the object model I want). The database contains, among many other things, a couple tables with a (0..)1-to-many constraint via foreign key like so:
go
create table Options (
KitNodeID int primary key foreign key references KitNodes (KitNodeID),
[SKUID] int null foreign key (SKUID) references SKUs (SKUID)
)
go
create table Upgrades (
UpgradeID int identity (1, 1) primary key not null,
[Name] nvarchar(50) not null,
DefaultOptionID int null references Options (KitNodeID)
)
The relevant sections in the EDMX look like this:
[...]
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="DModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="DModelStoreContainer">
<AssociationSet Name="FK__Upgrades__Defaul__70DDC3D8" Association="DModel.Store.FK__Upgrades__Defaul__70DDC3D8">
<End Role="Options" EntitySet="Options" />
<End Role="Upgrades" EntitySet="Upgrades" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Upgrades">
<Key>
<PropertyRef Name="UpgradeID" />
</Key>
<Property Name="UpgradeID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="50" />
<Property Name="DefaultOptionID" Type="int" />
</EntityType>
<Association Name="FK__Upgrades__Defaul__70DDC3D8">
<End Role="Options" Type="DModel.Store.Options" Multiplicity="0..1" />
<End Role="Upgrades" Type="DModel.Store.Upgrades" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Options">
<PropertyRef Name="KitNodeID" />
</Principal>
<Dependent Role="Upgrades">
<PropertyRef Name="DefaultOptionID" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema></edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="DModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="DEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Upgrades" EntityType="DModel.Upgrade" />
<AssociationSet Name="Upgrade_DefaultOption" Association="DModel.Upgrade_DefaultOption">
<End Role="Options" EntitySet="Options" />
<End Role="Upgrades" EntitySet="Upgrades" />
</AssociationSet>
</EntityContainer>
<Association Name="Upgrade_DefaultOption">
<End Role="Options" Type="DModel.Option" Multiplicity="0..1" />
<End Role="Upgrades" Type="DModel.Upgrade" Multiplicity="*" />
</Association>
<EntityType Name="Upgrade">
<Key>
<PropertyRef Name="UpgradeID" />
</Key>
<Property Name="UpgradeID" Nullable="false" annotation:StoreGeneratedPattern="Identity" Type="Int32" />
<Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" />
<NavigationProperty Name="DefaultOption" Relationship="DModel.Upgrade_DefaultOption" FromRole="Upgrades" ToRole="Options" />
<NavigationProperty Name="OptInOptions" Relationship="DModel.OptInOptions" FromRole="Upgrades" ToRole="Options" />
</EntityType>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
<EntityContainerMapping StorageEntityContainer="DModelStoreContainer" CdmEntityContainer="DEntities">
<EntitySetMapping Name="Upgrades"><EntityTypeMapping TypeName="DModel.Upgrade"><MappingFragment StoreEntitySet="Upgrades">
<ScalarProperty Name="UpgradeID" ColumnName="UpgradeID" />
<ScalarProperty Name="Name" ColumnName="Name" />
</MappingFragment></EntityTypeMapping></EntitySetMapping>
<AssociationSetMapping Name="Upgrade_DefaultOption" TypeName="DModel.Upgrade_DefaultOption" StoreEntitySet="FK__Upgrades__Defaul__70DDC3D8">
<EndProperty Name="Upgrades">
<ScalarProperty Name="UpgradeID" ColumnName="UpgradeID"/>
</EndProperty>
<EndProperty Name="Options">
<ScalarProperty Name="KitNodeID" ColumnName="DefaultOptionID"/>
</EndProperty>
</AssociationSetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
It will generate code, but when I try to use it I get an "Error 2007: The Table 'FK_Upgrades_Defaul__70DDC3D8' specified as part of this MSL does not exist in MetadataWorkspace." It looks like it can't find the underlying foreign key constraint that the SSDL is supposed to use, but I do see one with that name in the database.
Really, I feel I don't have a very good understanding of how this kind of mapping is supposed to work in general - is a foreign key constraint in the database really treated as an "associationset"? - but this is the closest I can work out. I don't know if anyone could diagnose the problem from the info I've given, but any pointers on where to look? I've tried various things like changing the StoreEntitySet to point to Options, etc. but they just result in different errors.
Try changing how you define your foreign key, see: How do I create a foreign key in SQL Server?
You will probably have to delete and recreate you model.